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

 

在Android中:

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

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

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

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

 

Code :

 

/**
 * 加载大图片工具类:解决android加载大图片时报OOM异常
 * 解决原理:先设置缩放选项,再读取缩放的图片数据到内存,规避了内存引起的OOM
 * @author: 张进 

 * @time:2011/7/28
 */
public class BitmapUtil {

    public static final int UNCONSTRAINED = -1;
    
    /*
  * 获得设置信息
  */
 public static Options getOptions(String path){
  Options options = new Options();
  options.inJustDecodeBounds = true;//只描边,不读取数据
  BitmapFactory.decodeFile(path, options);
  return options;
 }
 
 
 /**
  * 获得图像
  * @param path
  * @param options
  * @return
  * @throws FileNotFoundException
  */
 public static Bitmap getBitmapByPath(String path, Options options , int screenWidth , int screenHeight)throws FileNotFoundException{
  File file = new File(path);
  if(!file.exists()){
   throw new FileNotFoundException();
  }
  FileInputStream in = null;
  in = new FileInputStream(file);
  if(options != null){
   Rect r = getScreenRegion(screenWidth,screenHeight);
   int w = r.width();
   int h = r.height();
   int maxSize = w > h ? w : h;
   int inSimpleSize = computeSampleSize(options, maxSize, w * h);
   options.inSampleSize = inSimpleSize; //设置缩放比例
   options.inJustDecodeBounds = false;
  }
  Bitmap b = BitmapFactory.decodeStream(in, null, options);
  try {
   in.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return b;
 }
 
 
    
 private static Rect getScreenRegion(int width , int height) {
  return new Rect(0,0,width,height);
 }


 /**
  * 获取需要进行缩放的比例,即options.inSampleSize
  * @param options
  * @param minSideLength
  * @param maxNumOfPixels
  * @return
  */
 public static int computeSampleSize(BitmapFactory.Options options,
            int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength,
                maxNumOfPixels);

        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }

        return roundedSize;
    }

    private static int computeInitialSampleSize(BitmapFactory.Options options,
            int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;

        int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 :
                (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == UNCONSTRAINED) ? 128 :
                (int) Math.min(Math.floor(w / minSideLength),
                Math.floor(h / minSideLength));

        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }

        if ((maxNumOfPixels == UNCONSTRAINED) &&
                (minSideLength == UNCONSTRAINED)) {
            return 1;
        } else if (minSideLength == UNCONSTRAINED) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
    
 
}


 

 

工具类的使用:

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


 

 

 


 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值