[英]In Jelly Bean (Android 4.1) have error occured -> open failed: EMFILE (Too many open files)
[英]android EMFILE (Too many open files)
我實現了一個文件緩存來加載長圖像的小圖像。 滾動一段時間后,我得到了很多libcore.io.ErrnoException: open failed: EMFILE (Too many open files)
我該如何避免這種情況? 這是讀取一個位圖的代碼:
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();
由於異常,你的close()調用是否有機會? 我一般編碼如下:
try {
new FileOutputStream(fileUri);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
} finally {
try {
outputStream.close();
} catch (Exception ignored) {
// ignore exceptions generated by close()
}
}
我看不出第一個代碼片段有什么明顯錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.