2015-09-25 63 views
0

我做缩放位图如下图所示java.lang.OutOfMemoryError:无法分配与15257484个免费字节一个字节31961100分配和14MB直到OOM

Bitmap resizedBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(picturePath), 960, 730, false); 

这样处理我很上传图片有时我越来越低型的错误

java.lang.OutOfMemoryError: Failed to allocate a 31961100 byte allocation with 15257484 free bytes and 14MB until OOM 

请帮我解决如何?

+0

使用'BitmapFactory.Options'对象作为第二个参数'decodeFile()'的完整链接。将'BitmapFactory.Options'中的'inSampleSize'设置为一个值,以使您接近所需的缩放大小。 – CommonsWare

+1

您的操作需要比免费更多的内存。你对我们期望什么样的解决方案/帮助? – anderas

+0

@ CommonsWare请举例说明如何使用BitmapFactory.Options? – nag

回答

5

这是你如何使用BitmapFactory.Options

final BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
options.inSampleSize = 2; 
options.inJustDecodeBounds = false; 
options.inTempStorage = new byte[16 * 1024]; 

Bitmap bmp = BitmapFactory.decodeFile(picturePath,options); 
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bmp, 960, 730, false); 

您也可以通过编写自定义函数计算inSampleSize您的位图。

下面是谷歌文档:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

您可以在清单中加入android:largeHeap="true"增加分配给您的应用程序内存。

注意:增加heap为您的应用程序不认为是一个理想的解决方案。

下面是从谷歌的提取物,解释它,

However, the ability to request a large heap is intended only for a small set of apps that can justify the need to consume more RAM (such as a large photo editing app). Never request a large heap simply because you've run out of memory and you need a quick fix—you should use it only when you know exactly where all your memory is being allocated and why it must be retained. Yet, even when you're confident your app can justify the large heap, you should avoid requesting it to whatever extent possible. Using the extra memory will increasingly be to the detriment of the overall user experience because garbage collection will take longer and system performance may be slower when task switching or performing other common operations.

这里的文档https://developer.android.com/training/articles/memory.html

相关问题