图片在压缩过程中,OOM,bm = BitmapFactory.decodeFile(file.getPath(), opt); 是这一行报错,大神们帮忙想下办法吧,谢谢了!
BitmapFactory.Options opt = new BitmapFactory.Options();
// 这个isjustdecodebounds很重要
opt.inJustDecodeBounds = true;
Bitmap bm = BitmapFactory.decodeFile(file.getPath(), opt);
// 获取到这个图片的原始宽度和高度
int picWidth = opt.outWidth;
int picHeight = opt.outHeight;
// 获取屏的宽度和高度
WindowManager windowManager = context.getWindowManager();
Display display = windowManager.getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
// isSampleSize是表示对图片的缩放程度,比如值为2图片的宽度和高度都变为以前的1/2
//opt.inSampleSize = 1;
int a=0;
// 根据屏的大小和图片大小计算出缩放比例
if (picWidth > picHeight) {
if (picWidth > screenWidth)
//opt.inSampleSize = picWidth / screenWidth;
a= picWidth / screenWidth;
} else {
if (picHeight > screenHeight)
// opt.inSampleSize = picHeight / screenHeight;
a = picHeight / screenHeight;
}
if(a<=0){a=3;}
opt.inSampleSize = a;
// 这次再真正地生成一个有像素的,经过缩放了的bitmap
opt.inJustDecodeBounds = false;
try {
bm = BitmapFactory.decodeFile(file.getPath(), opt);
//压缩后的宽和高
int w = bm.getWidth();
int h = bm.getHeight();
return bm;
} catch (Exception e) {
e.printStackTrace();
return null;
}