我想从画廊(意图)获取图像 我收到了这个错误:
985120-byte external allocation too large for this process.
Out of memory: Heap Size=4871KB, Allocated=2472KB, Bitmap Size=19677KB
VM won't let us allocate 985120 bytes
这是我得到图像的代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
....
mBitmap = Media.getBitmap(this.getContentResolver(), data.getData());
...
}
我该如何解决?
--------更新---------
我注意到如果我选择一个预先存在的图像(安装了HTC照片),我会收到此错误。如果我选择从相机中拾取的图像,一切正常。
因此,我根据此http://developer.android.com/training/displaying-bitmaps/load-bitmap.html更改了我的代码:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream stream = getContentResolver().openInputStream(data.getData());
mBitmap = BitmapFactory.decodeStream(stream,null,options);
stream.close();
但现在位图是NULL !!!
答案 0 :(得分:1)
看起来您的应用程序使用了大量高分辨率位图(位图内存分区为19677KB)。 '堆'和'分配'的sie很正常,不应该有任何问题。确保从内存中删除未使用的位图。您可以通过调用bitmap.recycle()
或将对它的引用设置为null来从内存中释放位图。如果您想出于性能原因缓存位图,请查看LruCache。
答案 1 :(得分:0)
我总是在while循环中解码,增加inSampleSize并捕获OutOfMemoryError。这将为您提供最大可能的分辨率图像。始终使用LRU缓存!
Bitmap image;
boolean success = false;int counter = 0;
while (success == false && counter < 10)
{
try
{
image = BitmapFactory.decodeFile(photoPath, options);
success = true;
}
catch(OutOfMemoryError e)
{
System.gc();
options.inSampleSize++;
counter++;
}
}