通过BitmapFactory.decodeByteArray把byte[]转成Bitmap出现的OOM的解决方法

使用Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length)来转成Bitmap的时候,老报OOM,在网上也找了很多关于优化OOM的方法。

最后,采用了以下方法后,再也没有报OOM了


[java] view plain copy
  1. public static Bitmap byteToBitmap(byte[] imgByte) {  
  2.         InputStream input = null;  
  3.         Bitmap bitmap = null;  
  4.         BitmapFactory.Options options = new BitmapFactory.Options();  
  5.         options.inSampleSize = 8;  
  6.         input = new ByteArrayInputStream(imgByte);  
  7.         SoftReference softRef = new SoftReference(BitmapFactory.decodeStream(  
  8.                 input, null, options));  
  9.         bitmap = (Bitmap) softRef.get();  
  10.         if (imgByte != null) {  
  11.             imgByte = null;  
  12.         }  
  13.   
  14.         try {  
  15.             if (input != null) {  
  16.                 input.close();  
  17.             }  
  18.         } catch (IOException e) {  
  19.             // TODO Auto-generated catch block  
  20.             e.printStackTrace();  
  21.         }  
  22.         return bitmap;  
  23.     }  
原文地址:https://www.cnblogs.com/vegetate/p/9997255.html