I implemented a file cache to load small images for a long grid view. After scrolling a while, I get a lot of libcore.io.ErrnoException: open failed: EMFILE (Too many open files)
How do I avoid this? This is the code to read one bitmap:
File fullCacheDir = new File(Environment.getExternalStorageDirectory().toString(), cacheDir);
File file = new File(fullCacheDir.toString(), fileName);
if (!file.exists()) {
return null;
}
Bitmap bm = BitmapFactory.decodeFile(file.toString());
This is to save one bitmap:
FileOutputStream outputStream = new FileOutputStream(fileUri);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
Any chance your close() call isn’t being reached due to an exception? I generally code like this:
I couldn’t see anything obviously wrong with the first code fragment.