bitmapfactory.decodefile 内存溢出(OOM)问题解决方案

来源:互联网 发布:windows nas 方案 编辑:程序博客网 时间:2024/05/21 01:32

今天遇到一个OOM问题,上网查找相关资料,最后写了一个方法,以供参考:

//absolutePath是图片绝对路径private void adjustImage(String absolutePath,Bitmap bm) {BitmapFactory.Options opt = new BitmapFactory.Options();// 这个isjustdecodebounds很重要opt.inJustDecodeBounds = true;bm = BitmapFactory.decodeFile(absolutePath, opt);// 获取到这个图片的原始宽度和高度int picWidth = opt.outWidth;int picHeight = opt.outHeight;// 获取屏的宽度和高度WindowManager windowManager = getWindowManager();Display display = windowManager.getDefaultDisplay();int screenWidth = display.getWidth();int screenHeight = display.getHeight();// isSampleSize是表示对图片的缩放程度,比如值为2图片的宽度和高度都变为以前的1/2opt.inSampleSize = 1;// 根据屏的大小和图片大小计算出缩放比例if (picWidth > picHeight) {if (picWidth > screenWidth)opt.inSampleSize = picWidth / screenWidth;} else {if (picHeight > screenHeight)opt.inSampleSize = picHeight / screenHeight;}// 这次再真正地生成一个有像素的,经过缩放了的bitmapopt.inJustDecodeBounds = false;bm= BitmapFactory.decodeFile(absolutePath, opt);// 用imageview显示出bitmapimageView.setImageBitmap(bm);}


0 0
原创粉丝点击