在最后一行我遇到了崩溃:
InputStream in = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[(int) file.length()];
int numRead = in.read(buf);
final Bitmap bitmap = BitmapFactory.decodeByteArray(buf, 0, numRead); <--- crash
如何避免?
E/dalvikvm-heap: Out of memory on a 31961104-byte allocation.
我明白 31MB 是使用内存的限制吗?用于LRUCache
存储图像。我设置了 60MB 来存储数据。是不是太多了?
public static int cacheSize = 60 * 1024 * 1024; // 4MiB
public static LruCache<String, Bitmap> images = new LruCache<String, Bitmap>(cacheSize) {
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
在我的例子中,一个图像大约是 3MB,我需要至少 18 个图像存储在缓存中。手机存储60MB的需求这么大吗?
我尝试了代码,为什么应用程序终止?