安卓EMFILE(打开的文件太多) [英] android EMFILE (Too many open files)

查看:666
本文介绍了安卓EMFILE(打开的文件太多)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个文件缓存加载小图像久网格视图。滚动一段时间后,我得到了很多 libcore.io.ErrnoException的:打开失败:EMFILE(打开的文件太多)

我如何避免这种情况?这是code来读取一个位图:

 文件fullCacheDir =新的文件(Environment.getExternalStorageDirectory()的toString(),cacheDir。);
档案文件=新的文件(fullCacheDir.toString(),文件名);

如果(!file.exists()){
    返回null;
}

位图BM = BitmapFactory.de codeFILE(file.toString());
 

这是保存在一个位图:

 的FileOutputStream的OutputStream =新的FileOutputStream(了fileURI);
bitmap.com preSS(Bitmap.Com pressFormat.JPEG,100的OutputStream);
outputStream.flush();
outputStream.close();
 

解决方案

未因达到异常的接近()调用任何机会呢?我一般$这样的C $ C:

 尝试{
  新的FileOutputStream(了fileURI);
  bitmap.com preSS(Bitmap.Com pressFormat.JPEG,100的OutputStream);
  outputStream.flush();
} 最后 {
  尝试 {
    outputStream.close();
  }赶上(例外五){
    //忽略了近生成的异常()
  }
}
 

我什么也看不到明显的错误与第一code片段。

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:

try {
  new FileOutputStream(fileUri);
  bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
  outputStream.flush();
} finally {
  try {
    outputStream.close();
  } catch (Exception e) {
    // ignore exceptions generated by close()
  }
}

I couldn't see anything obviously wrong with the first code fragment.

这篇关于安卓EMFILE(打开的文件太多)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆