2015-09-25 53 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

相關問題