Android 优化Bitmap避免 OutOfMemoryError

来源:互联网 发布:棉被可以抽真空吗 知乎 编辑:程序博客网 时间:2024/06/08 00:44

使用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. }  
原创粉丝点击