2015/11/03

Android 解決Bitmap所產生的OutOfMemoryError

以下方法看情況使用啊,不是每種方法都可以用XD

方法一:再Manifest加入
android:largeHeap="true"

方法二:透過Stream讀取
ByteArrayInputStream stream = new ByteArrayInputStream(data);
Bitmap bitmap = BitmapFactory.decodeStream(stream);
stream.close();
stream = null;

方法三:透過Bitmap的recycle()方法釋放Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
bitmap.recycle();
bitmap = null;

方法四:透過BitmapFactory.Options將圖縮小至原來的1/n
ByteArrayInputStream stream = new ByteArrayInputStream(data);
BitmapFactory.Options options = new BitmapFactory.Options();
// 1/10
options.inSampleSize = 10;
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);


方法五:透過VMRuntime設定Memory大小,僅只2.3以下適用



private final static int SIZE = 10 * 1024 * 1024;
VMRuntime.getRuntime().setMinimumHeapSize(SIZE);