3
我實現了一個文件緩存來爲長網格視圖加載小圖像。滾動一段時間後,我收到很多libcore.io.ErrnoException: open failed: EMFILE (Too many open files)
android EMFILE(太多打開的文件)
我該如何避免這種情況?這是代碼讀取一個位圖:
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());
這是爲了保存一個位圖:
FileOutputStream outputStream = new FileOutputStream(fileUri);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
只是一個更新:Apache Commons有一個[closeQuietly()](https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/IOUtils.html#closeQuietly(java .io.Closeable))將關閉Closeable併吞下異常的實用程序。我覺得這很有用,即使Apache Commons不可用時,我也會[自己動手](http://www.efalk.org/Docs/Java/techniques.html#Close_Quietly) – 2016-04-05 22:42:06