加载大图片内存溢出的解决办法

  1. public static Bitmap fitSizeImg(String path) {
  2.   if(path == null || path.length()<1 ) return null;
  3.   File file = new File(path);
  4.   Bitmap resizeBmp = null;
  5.   BitmapFactory.Options opts = new BitmapFactory.Options();
  6.   // 数字越大读出的图片占用的heap越小 不然总是溢出
  7.   if (file.length() < 20480) {       // 0-20k
  8.    opts.inSampleSize = 1;
  9.   } else if (file.length() < 51200) { // 20-50k
  10.    opts.inSampleSize = 2;
  11.   } else if (file.length() < 307200) { // 50-300k
  12.    opts.inSampleSize = 4;
  13.   } else if (file.length() < 819200) { // 300-800k
  14.    opts.inSampleSize = 6;
  15.   } else if (file.length() < 1048576) { // 800-1024k
  16.    opts.inSampleSize = 8;
  17.   } else {
  18.    opts.inSampleSize = 10;
  19.   }
  20.   resizeBmp = BitmapFactory.decodeFile(file.getPath(), opts);
  21.   return resizeBmp;
  22. }

 

===============================================================

opts.inSampleSize 指定加载图片时,按照原尺寸大小的高宽的1/inSampleSize大小缩放。
如果此值大于1,相当于缩小,小于1时放大。

你可能感兴趣的:(加载大图片内存溢出的解决办法)