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

方法一:

/**
 * 加载大图片工具类:解决android加载大图片时报OOM异常 解决原理:先设置缩放选项,
 * 再读取缩放的图片数据到内存,规避了内存引起的OOM
 */
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;
}
}

}

使用: bitmap = BitmapUtil.getBitmapByPath(photoPath, BitmapUtil.getOptions(photoPath), photoWidth, photoHeight);

方法二:

// 按图片大小(字节大小)缩放图片   网友处理方式
 public static Bitmap fitSizeImg(String path) {
  if(path == null || path.length()<1 ) return null;
  File file = new File(path);
  Bitmap resizeBmp = null;
  BitmapFactory.Options opts = new BitmapFactory.Options();
  // 数字越大读出的图片占用的heap越小 不然总是溢出
  if (file.length() < 20480) {       // 0-20k
   opts.inSampleSize = 1;
  } else if (file.length() < 51200) { // 20-50k
   opts.inSampleSize = 2;
  } else if (file.length() < 307200) { // 50-300k
   opts.inSampleSize = 4;
  } else if (file.length() < 819200) { // 300-800k
   opts.inSampleSize = 6;
  } else if (file.length() < 1048576) { // 800-1024k
   opts.inSampleSize = 8;
  } else {
   opts.inSampleSize = 10;
  }
  resizeBmp = BitmapFactory.decodeFile(file.getPath(), opts);
  return resizeBmp;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值