完美解決java.lang.OutOfMemoryError: bitmap size exceeds VM budget

來源:互聯網
上載者:User

轉自http://www.maxhis.info/androiding/bitmap-size-exceed/

當圖片過大,或圖片數量較多時使用BitmapFactory解碼圖片會出java.lang.OutOfMemoryError: bitmap size exceeds VM budget,要想正常使用則需分配更少的記憶體,具體的解決辦法是修改採樣值BitmapFactory.Options.inSampleSize,例 如:Java代碼  

  1. BitmapFactory.Options opts = new BitmapFactory.Options();  
  2. opts.inSampleSize = 4;  
  3. Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);  

如何設定恰當的inSampleSize
設定恰當的inSampleSize是解決該問題的關鍵之一。BitmapFactory.Options提供了另一個成員inJustDecodeBounds。Java代碼  

  1. BitmapFactory.Options opts = new BitmapFactory.Options();  
  2. opts.inJustDecodeBounds = true;  
  3. Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);  

設定inJustDecodeBounds為true後,decodeFile並不分配空間,但可計算出原始圖片的長度和寬度,即opts.width和opts.height。有了這兩個參數,再通過一定的演算法,即可得到一個恰當的inSampleSize。
查看Android源碼,Android提供了一種動態計算的方法。Java代碼  

  1. public static int computeSampleSize(BitmapFactory.Options options,  
  2.         int minSideLength, int maxNumOfPixels) {  
  3.     int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);  
  4.   
  5.     int roundedSize;  
  6.     if (initialSize <= 8 ) {  
  7.         roundedSize = 1;  
  8.         while (roundedSize < initialSize) {  
  9.             roundedSize <<= 1;  
  10.         }  
  11.     } else {  
  12.         roundedSize = (initialSize + 7) / 8 * 8;  
  13.     }  
  14.   
  15.     return roundedSize;  
  16. }  
  17.   
  18. private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {  
  19.     double w = options.outWidth;  
  20.     double h = options.outHeight;  
  21.   
  22.     int lowerBound = (maxNumOfPixels == -1) ? 1 :  
  23.             (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));  
  24.     int upperBound = (minSideLength == -1) ? 128 :  
  25.             (int) Math.min(Math.floor(w / minSideLength),  
  26.             Math.floor(h / minSideLength));  
  27.   
  28.     if (upperBound < lowerBound) {  
  29.         // return the larger one when there is no overlapping zone.  
  30.         return lowerBound;  
  31.     }  
  32.   
  33.     if ((maxNumOfPixels == -1) &&  
  34.             (minSideLength == -1)) {  
  35.         return 1;  
  36.     } else if (minSideLength == -1) {  
  37.         return lowerBound;  
  38.     } else {  
  39.         return upperBound;  
  40.     }  
  41. }  

使用該演算法,就可動態計算出圖片的inSampleSize。Java代碼  

  1. BitmapFactory.Options opts = new BitmapFactory.Options();  
  2. opts.inJustDecodeBounds = true;  
  3. BitmapFactory.decodeFile(imageFile, opts);  
  4.   
  5. opts.inSampleSize = computeSampleSize(opts, -1, 128*128);  
  6. opts.inJustDecodeBounds = false;  
  7. try {  
  8.  Bitmap bmp = BitmapFactory.decodeFile(imageFile, opts);  
  9.  imageView.setImageBitmap(bmp);  
  10.     } catch (OutOfMemoryError err) {  
  11. }  

另外,可以通過

Java代碼  
  1. Bitmap.recycle()  

方法來釋放位元影像所佔的空間,當然前提是位元影像沒有被使用。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.