简体   繁体   中英

Android reading large Files (out of memory exception)

I'm currently trying to read large files (>14MB) into my Android App. Here is the code I'm currently using:

AssetManager manager = context.getAssets();
InputStream stream = manager.open(Filename);
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
Scontent = br.readLine();  //I just load one big line that is bigger than 14 MB

But I'm getting an out of memory exception on a ~14MB allocation. I read something about a maximum app size of 16MB (depending on the device), but I can't find a good way to load that much data.

What would be the easiest solution to load such big files?

instead of using a big files use multiple smaller files (~1MB). Then read those sequentially if you need to read all data at a time

The InputStreamReader class will let you read the file byte by byte. You're already using that class, however you're embedding it into a BufferedReader class. So discard the BufferedReader and read your big file byte by byte youself. If you do that, and process the bytes as you go, then you won't need to read the whole file in a single operation.

您可以按字符读取char,但是可以再次读取,但是请注意在使用此较小的块后,有关分配的引用会移除,因此Garbage Cleaner可以返回该内存。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM