閱讀181 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Android 優化Bitmap避免 OutOfMemoryError

使用android提供的BitmapFactory解碼圖片時,往往會因為圖片過大而遇到OutOfMemoryError的異常。要想正常使用,一種簡便的方式是分配更少的內存空間來存儲,即在載入圖片的時候以犧牲圖片質量為代價,將圖片進行放縮,這是一種避免OOM所采用的解決方法。但是,這種方法是得不償失的,犧牲了圖片質量。

在BitmapFactory中有一個內部類BitmapFactory.Options,其中值得我們注意的是inSampleSize和inJustDecodeBounds兩個屬性:

    inSampleSize是以2的指數的倒數被進行放縮

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.

inJustDecodeBounds為Boolean型

設置inJustDecodeBounds為true後,decodeFile並不分配空間,但可計算出原始圖片的長度和寬度,即options.outWidth和options.outHeight。

要對圖片進行縮放,最大的問題就是怎麼在運行時動態的改變inSampleSize的值,通過上麵的inJustDecodeBounds可以知道圖片原始的大小,那麼這樣以來就可以通過算法來得到一個恰當的inSampleSize值。其動態算法可參考下麵的,網上也很多,大體都一樣:

 

Java代碼  收藏代碼
  1. /** 
  2.  * compute Sample Size 
  3.  *  
  4.  * @param options 
  5.  * @param minSideLength 
  6.  * @param maxNumOfPixels 
  7.  * @return 
  8.  */  
  9. public static int computeSampleSize(BitmapFactory.Options options,  
  10.         int minSideLength, int maxNumOfPixels) {  
  11.     int initialSize = computeInitialSampleSize(options, minSideLength,  
  12.             maxNumOfPixels);  
  13.   
  14.     int roundedSize;  
  15.     if (initialSize <= 8) {  
  16.         roundedSize = 1;  
  17.         while (roundedSize < initialSize) {  
  18.             roundedSize <<= 1;  
  19.         }  
  20.     } else {  
  21.         roundedSize = (initialSize + 7) / 8 * 8;  
  22.     }  
  23.   
  24.     return roundedSize;  
  25. }  
  26.   
  27. /** 
  28.  * compute Initial Sample Size 
  29.  *  
  30.  * @param options 
  31.  * @param minSideLength 
  32.  * @param maxNumOfPixels 
  33.  * @return 
  34.  */  
  35. private static int computeInitialSampleSize(BitmapFactory.Options options,  
  36.         int minSideLength, int maxNumOfPixels) {  
  37.     double w = options.outWidth;  
  38.     double h = options.outHeight;  
  39.   
  40.     // 上下限範圍  
  41.     int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math  
  42.             .sqrt(w * h / maxNumOfPixels));  
  43.     int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(  
  44.             Math.floor(w / minSideLength), Math.floor(h / minSideLength));  
  45.   
  46.     if (upperBound < lowerBound) {  
  47.         // return the larger one when there is no overlapping zone.  
  48.         return lowerBound;  
  49.     }  
  50.   
  51.     if ((maxNumOfPixels == -1) && (minSideLength == -1)) {  
  52.         return 1;  
  53.     } else if (minSideLength == -1) {  
  54.         return lowerBound;  
  55.     } else {  
  56.         return upperBound;  
  57.     }  
  58. }  

 

 有了上麵的算法,我們就可以輕易的get到Bitmap了:

Java代碼  收藏代碼
  1. /** 
  2.  * get Bitmap 
  3.  *  
  4.  * @param imgFile 
  5.  * @param minSideLength 
  6.  * @param maxNumOfPixels 
  7.  * @return 
  8.  */  
  9. public static Bitmap tryGetBitmap(String imgFile, int minSideLength,  
  10.         int maxNumOfPixels) {  
  11.     if (imgFile == null || imgFile.length() == 0)  
  12.         return null;  
  13.   
  14.     try {  
  15.         FileDescriptor fd = new FileInputStream(imgFile).getFD();  
  16.         BitmapFactory.Options options = new BitmapFactory.Options();  
  17.         options.inJustDecodeBounds = true;  
  18.         // BitmapFactory.decodeFile(imgFile, options);  
  19.         BitmapFactory.decodeFileDescriptor(fd, null, options);  
  20.   
  21.         options.inSampleSize = computeSampleSize(options, minSideLength,  
  22.                 maxNumOfPixels);  
  23.         try {  
  24.             // 這裏一定要將其設置回false,因為之前我們將其設置成了true  
  25.             // 設置inJustDecodeBounds為true後,decodeFile並不分配空間,即,BitmapFactory解碼出來的Bitmap為Null,但可計算出原始圖片的長度和寬度  
  26.             options.inJustDecodeBounds = false;  
  27.   
  28.             Bitmap bmp = BitmapFactory.decodeFile(imgFile, options);  
  29.             return bmp == null ? null : bmp;  
  30.         } catch (OutOfMemoryError err) {  
  31.             return null;  
  32.         }  
  33.     } catch (Exception e) {  
  34.         return null;  
  35.     }  
  36. }  

最後更新:2017-04-02 17:09:28

  上一篇:go Android【 ListView】滑動數據加載
  下一篇:go 關於ISO-8859-1編碼