bitmap溢出oom问题的原因以及解决
来源:互联网 发布:linux切换登录用户命令 编辑:程序博客网 时间:2024/05/16 18:00
很多人在android开发中都遇到了生成bitmap时候内存溢出,也就是out of memory(OOM)的问题,网上对这样的问题的的解决说法不一。在这里向大家提供一种比较实用,比较易于理解的方法,这种方法不如一些高级开发者提出的方案来的深刻,但是也能帮助大家有效地解决问题。
废话不多说了,直接上代码。
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/2
opt.inSampleSize = 1;
//根据屏的大小和图片大小计算出缩放比例
if(picWidth > picHeight){
if(picWidth > screenWidth)
opt.inSampleSize = picWidth/screenWidth;
}
else{
if(picHeight > screenHeight)
opt.inSampleSize = picHeight/screenHeight;
}
//这次再真正地生成一个有像素的,经过缩放了的bitmap
opt.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(absolutePath, opt);
//用imageview显示出bitmap
iv.setImageBitmap(bm);
inJustDecodeBounds 的介绍
public boolean inJustDecodeBounds
Since: API Level 1
If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.
就是说,如果设置inJustDecodeBounds为true,仍可以获取到bitmap信息,但完全不用分配内存,因为没有获取像素,所以我们可以利用得到的Bitmap的大小,重新压缩图片,然后在内存中生成一个更小的Bitmap,这样即便是一个4MB的JPG,我们也可以随心所欲地把他压缩到任意大小,从而节省了内存。
下面这个参数就是跟压缩图片有关的部分,很容易懂,不多解释了:
inSampleSize 的介绍
public int inSampleSize
Since: API Level 1
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value
inSampleSize是压缩比例,例如 inSampleSize == 4 ,就是长宽为原图的1/4 面积为原图的1/16。
- bitmap溢出oom问题的原因以及解决
- android 解决bitmap造成的OOM问题
- 测试Perm区溢出引起的OOM以及原因分析
- bitmap的oom问题
- 解决Bitmap中的OOM问题
- 解决Bitmap导致的内存溢出问题
- bitmap 内存溢出OOM的解决办法分享
- bitmap的生成,以及引起的OOM问题总结
- Android学习札记24:收集到的一些关于解决Bitmap OOM内存溢出的方法
- 收集到的一些关于解决Bitmap OOM内存溢出的方法
- Android学习札记24:收集到的一些关于解决Bitmap OOM内存溢出的方法
- 如何解决加载大图片时内存溢出的问题 OOM
- bitmap溢出的问题
- Android使用bitmap导致内存溢出(oom)问题
- 解决Bitmap溢出的方法
- android Bitmap过大内存溢出问题的解决
- Android的Bitmap内存溢出解决方案(OOM) 整理总结
- 解决内存溢出(OOM)的思考
- kafka_2.9.2-0.8.1.1安装
- Navicat for Oracle的表触发器有哪些属性
- xcode配置包路径
- 手机web——自适应网页设计(html/css控制)
- linux下基于jrtplib库的实时传送实现
- bitmap溢出oom问题的原因以及解决
- 127. Word Ladder
- 树状数组
- 用C语言完成简单的计算器功能(1)
- hadoop调试技巧--解除已经绑定的端口
- Android自定义没有资源文件的圆形进度条ProgressBar
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
- (一)Mina源码解析之整体架构
- android安卓手机分屏多窗口实现方法