我想将images
放入dictionary
。图像总大小为22兆字节。这是我的代码。
Dictionary<String, Bitmap> bmpCollection = new Hashtable<String, Bitmap>();
File filesDirectory = new File(Environment.getExternalStorageDirectory().getPath() + "/shared/Lenovo/Resimler/");
File[] files = filesDirectory.listFiles();
for (File file:files)
{
try {
Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
String key = file.getName();
key = key.substring(0,key.length()-4);
bmpCollection.put(key,bmp);
}
catch (Exception ex)
{
int a = 0;
}
}
return bmpCollection;
在第48个文件之前没有问题,但在该行程序Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
之后该程序重启之后没有问题。它没有说错误。不要触发catch
。只需重新启动应用程序。可能是什么问题,我该如何解决?
答案 0 :(得分:2)
可能是内存不足错误。尝试使用purgeable和samplesize位图选项进行解码以优化内存:
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inPurgeable = true;
bmOptions.inSampleSize = 2;
Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath(), bmOptions);