網上有不少解決android加載bitmap內存溢出的方法,搜了一圈作下整理總結。項目裏需求是拍攝多圖以後上傳,部分手機會內存溢出。html
經常使用一種解決方法:即將載入的圖片縮小,這種方式以犧牲圖片的質量爲代價。在BitmapFactory中有一個內部類BitmapFactory.Options,其中當options.inSampleSize值>1時,根據文檔:java
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. (1 -> decodes full size; 2 -> decodes 1/4th size; 4 -> decode 1/16th size). Because you rarely need to show and have full size bitmap images on your phone. For manipulations smaller sizes are usually enough.
options.inSampleSize是以2的指數的倒數被進行放縮
如今問題是怎麼肯定inSampleSize的值?每張圖片的放縮大小的比例應該是不同的!這樣的話就要運行時動態肯定。在BitmapFactory.Options中提供了另外一個成員inJustDecodeBounds。
設置inJustDecodeBounds爲true後,decodeFile並不分配空間,但可計算出原始圖片的長度和寬度,即opts.width和opts.height。有了這兩個參數,再經過必定的算法,便可獲得一個恰當的inSampleSize。Android提供了一種動態計算的方法,見computeSampleSize().android
public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels); int roundedSize; if (initialSize <= 8) { roundedSize = 1; while (roundedSize < initialSize) { roundedSize <<= 1; } } else { roundedSize = (initialSize + 7) / 8 * 8; } return roundedSize; } private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { double w = options.outWidth; double h = options.outHeight; int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels)); int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength)); if (upperBound < lowerBound) { return lowerBound; } if ((maxNumOfPixels == -1) && (minSideLength == -1)) { return 1; } else if (minSideLength == -1) { return lowerBound; } else { return upperBound; } }
以上只作爲參考,咱們只要用這函數便可,opts.inSampleSize = computeSampleSize(opts, -
1
,
128
*
128
);
算法
要點:
一、用decodeFileDescriptor()來生成bimap比decodeFile()省內存app
FileInputStream is = = new FileInputStream(path); bmp = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts);
替換ide
Bitmap bmp = BitmapFactory.decodeFile(imageFile, opts); imageView.setImageBitmap(bmp);
緣由:
查看BitmapFactory的源碼,對比一下二者的實現,能夠發現decodeFile()最終是以流的方式生成bitmap 函數
decodeFile源碼:spa
public static Bitmap decodeFile(String pathName, Options opts) { Bitmap bm = null; InputStream stream = null; try { stream = new FileInputStream(pathName); bm = decodeStream(stream, null, opts); } catch (Exception e) { /* do nothing. If the exception happened on open, bm will be null. */ } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { // do nothing here } } } return bm; }
decodeFileDescriptor的源碼,能夠找到native本地方法decodeFileDescriptor,經過底層生成bitmapcode
decodeFileDescriptor源碼:orm
public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts) { if (nativeIsSeekable(fd)) { Bitmap bm = nativeDecodeFileDescriptor(fd, outPadding, opts); if (bm == null && opts != null && opts.inBitmap != null) { throw new IllegalArgumentException("Problem decoding into existing bitmap"); } return finishDecode(bm, outPadding, opts); } else { FileInputStream fis = new FileInputStream(fd); try { return decodeStream(fis, outPadding, opts); } finally { try { fis.close(); } catch (Throwable t) {/* ignore */} } } } private static native Bitmap nativeDecodeFileDescriptor(FileDescriptor fd,Rect padding, Options opts);
二、當在android設備中載入較大圖片資源時,能夠建立一些臨時空間,將載入的資源載入到臨時空間中。
opts.inTempStorage = new byte[16 * 1024];
完整代碼:
public static OutputStream decodeBitmap(String path) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true;// 設置成了true,不佔用內存,只獲取bitmap寬高 BitmapFactory.decodeFile(path, opts); opts.inSampleSize = computeSampleSize(opts, -1, 1024 * 800); opts.inJustDecodeBounds = false;// 這裏必定要將其設置回false,由於以前咱們將其設置成了true opts.inPurgeable = true; opts.inInputShareable = true; opts.inDither = false; opts.inPurgeable = true; opts.inTempStorage = new byte[16 * 1024]; FileInputStream is = null; Bitmap bmp = null; InputStream ins = null; ByteArrayOutputStream baos = null; try { is = new FileInputStream(path); bmp = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts); double scale = getScaling(opts.outWidth * opts.outHeight, 1024 * 600); Bitmap bmp2 = Bitmap.createScaledBitmap(bmp, (int) (opts.outWidth * scale), (int) (opts.outHeight * scale), true); bmp.recycle(); baos = new ByteArrayOutputStream(); bmp2.compress(Bitmap.CompressFormat.JPEG, 100, baos); bmp2.recycle(); return baos; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); ins.close(); baos.close(); } catch (IOException e) { e.printStackTrace(); } System.gc(); } return baos; } private static double getScaling(int src, int des) { /** * 目標尺寸÷原尺寸 sqrt開方,得出寬高百分比 */ double scale = Math.sqrt((double) des / (double) src); return scale; }