方法一:再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);