解决加载大图片出现的oom
<span style="white-space:pre"> </span>//解析图片时需要使用到的参数都封装在这个对象里了
Options opt = new Options();
//不为像素申请内存,只获取图片宽高
opt.inJustDecodeBounds = true;
BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath() + "/dog.jpg", opt);
//拿到图片宽高
int imageWidth = opt.outWidth;
int imageHeight = opt.outHeight;
Display dp = getWindowManager().getDefaultDisplay();
//拿到屏幕宽高
int screenWidth = dp.getWidth();
int screenHeight = dp.getHeight();
//计算缩放比例
int scale = 1;
int scaleWidth = imageWidth / screenWidth;
int scaleHeight = imageHeight / screenHeight;
if(scaleWidth >= scaleHeight && scaleWidth >= 1){
scale = scaleWidth;
}
else if(scaleWidth < scaleHeight && scaleHeight >= 1){
scale = scaleHeight;
}
//设置缩放比例
opt.inSampleSize = scale;
opt.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeFile("sdcard/dog.jpg", opt);
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setImageBitmap(bm);