您的位置:首页 > 其它

获取手机摄像机图片的缩略图方法,防止内存溢出异常

2016-12-07 10:25 204 查看
imgIv.setImageBitmap(getImageThumbnail(uri,200,200));//调用方法,传入数据


//具体方法

// 获取相机照片的缩略图,直接获取原始图片会触发系统 oom错误
public Bitmap getImageThumbnail(String uri, int width, int height) {
//72-75行是为了防止发生图片过大导致内存溢出错误,用BitmapFactory.Options的方法,得到bitmap=要取出图片的宽和高信息
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeFile(uri, options);
options.inJustDecodeBounds = false;//取出bitmap信息之后关闭options.inJustDecodeBounds,与上面成对出现

int beWidth = options.outWidth / width;
int beHeight = options.outHeight / height;
int be = 1;
if (beWidth < beHeight) {
be = beWidth;
} else {
be = beHeight;
}
if (be <= 0) {
be = 1;
}
options.inSampleSize = be;
bitmap = BitmapFactory.decodeFile(uri, options);//根据uri地址取出bitmap,同时options不为空时,这个bitmap将按照前面这只的比例显示
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
return bitmap;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息