Android 加载大图片时报OOM的解决方案(源码)


在Android中:

  1.一个进程的内存可以由2个部门组成:java 施用内存 ,C 施用内存 ,这两个内存的和必需小于16M,不然就会出现各人熟悉的OOM,这个就是熬头种OOM的情况。

  2.一朝内存分配给Java后,以后这块内存纵然开释后,也只能给Java的施用,这个估计跟java虚拟机里把内存分成好几块进行缓存的原因有关,反正C就别想用到这块的内存了,所以要是Java突然占用了一个大块内存,纵然很快开释了:

  C能施用的内存 = 16M - Java某一瞬间占在校大学生创业点子用的最大内存。

  而Bitmap的生成是路程经过过程malloc进行内存分配的,占用的是C的内存。

Code :

 
  1. /** 
  2.  * 加载大图片工具类:解决android加载大图片时报OOM异常 
  3.  * 解决原理:先设置缩放选项,再读取缩放的图片数据到内存,规避了内存引起的OOM 
  4.  * @author: 张进  
  5.  
  6.  * @time:2011/7/28 
  7.  */  
  8. public class BitmapUtil {  
  9.   
  10.     public static final int UNCONSTRAINED = -1;  
  11.       
  12.     /* 
  13.   * 获得设置信息 
  14.   */  
  15.  public static Options getOptions(String path){  
  16.   Options options = new Options();  
  17.   options.inJustDecodeBounds = true;//只描边,不读取数据   
  18.   BitmapFactory.decodeFile(path, options);  
  19.   return options;  
  20.  }  
  21.    
  22.    
  23.  /** 
  24.   * 获得图像 
  25.   * @param path 
  26.   * @param options 
  27.   * @return 
  28.   * @throws FileNotFoundException 
  29.   */  
  30.  public static Bitmap getBitmapByPath(String path, Options options , int screenWidth , int screenHeight)throws FileNotFoundException{  
  31.   File file = new File(path);  
  32.   if(!file.exists()){  
  33.    throw new FileNotFoundException();  
  34.   }  
  35.   FileInputStream in = null;  
  36.   in = new FileInputStream(file);  
  37.   if(options != null){  
  38.    Rect r = getScreenRegion(screenWidth,screenHeight);  
  39.    int w = r.width();  
  40.    int h = r.height();  
  41.    int maxSize = w > h ? w : h;  
  42.    int inSimpleSize = computeSampleSize(options, maxSize, w * h);  
  43.    options.inSampleSize = inSimpleSize; //设置缩放比例   
  44.    options.inJustDecodeBounds = false;  
  45.   }  
  46.   Bitmap b = BitmapFactory.decodeStream(in, null, options);  
  47.   try {  
  48.    in.close();  
  49.   } catch (IOException e) {  
  50.    e.printStackTrace();  
  51.   }  
  52.   return b;  
  53.  }  
  54.    
  55.    
  56.       
  57.  private static Rect getScreenRegion(int width , int height) {  
  58.   return new Rect(0,0,width,height);  
  59.  }  
  60.   
  61.   
  62.  /** 
  63.   * 获取需要进行缩放的比例,即options.inSampleSize 
  64.   * @param options 
  65.   * @param minSideLength 
  66.   * @param maxNumOfPixels 
  67.   * @return 
  68.   */  
  69.  public static int computeSampleSize(BitmapFactory.Options options,  
  70.             int minSideLength, int maxNumOfPixels) {  
  71.         int initialSize = computeInitialSampleSize(options, minSideLength,  
  72.                 maxNumOfPixels);  
  73.   
  74.         int roundedSize;  
  75.         if (initialSize <= 8) {  
  76.             roundedSize = 1;  
  77.             while (roundedSize < initialSize) {  
  78.                 roundedSize <<= 1;  
  79.             }  
  80.         } else {  
  81.             roundedSize = (initialSize + 7) / 8 * 8;  
  82.         }  
  83.   
  84.         return roundedSize;  
  85.     }  
  86.   
  87.     private static int computeInitialSampleSize(BitmapFactory.Options options,  
  88.             int minSideLength, int maxNumOfPixels) {  
  89.         double w = options.outWidth;  
  90.         double h = options.outHeight;  
  91.   
  92.         int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 :  
  93.                 (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));  
  94.         int upperBound = (minSideLength == UNCONSTRAINED) ? 128 :  
  95.                 (int) Math.min(Math.floor(w / minSideLength),  
  96.                 Math.floor(h / minSideLength));  
  97.   
  98.         if (upperBound < lowerBound) {  
  99.             // return the larger one when there is no overlapping zone.   
  100.             return lowerBound;  
  101.         }  
  102.   
  103.         if ((maxNumOfPixels == UNCONSTRAINED) &&  
  104.                 (minSideLength == UNCONSTRAINED)) {  
  105.             return 1;  
  106.         } else if (minSideLength == UNCONSTRAINED) {  
  107.             return lowerBound;  
  108.         } else {  
  109.             return upperBound;  
  110.         }  
  111.     }  
  112.       
  113.    
  114. }  

工具类的使用:

 
  1. String path = "/sdcard/test2.jpg";  
  2.     try {  
  3.   Bitmap bitmap = BitmapUtil.getBitmapByPath(path, BitmapUtil.getOptions(path), screenWidth, screenHeight);  
  4.  } catch (FileNotFoundException e) {  
  5.   e.printStackTrace();  
  6.  }  

相关内容