很多人在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,我们也可以随心所欲地把他压缩到任意大小,从而节省了内存,看到这里是不是恍然大悟,牛b了牛b了!
下面这个参数就是跟压缩图片有关的部分,很容易懂,不多解释了:
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
分享到:
相关推荐
android bitmap outofMemory 用来解决android中常见的bitmap outOfMemory
处理bitmap内存溢出问题
ANDROIDBITMAP内存限制OOM,OUTOFMEMORY.pdf
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts); 设置inJustDecodeBounds为true后,decodeFile并不分配空间,但可计算出原始图片的长度和宽度,即opts.width和opts.height。有了这两个参数,再通过...
ANDROIDBITMAP内存限制OOM,OUTOFMEMORY[文].pdf
Android中bitmap引起内存溢出的解决方案的对比分析.pdf
可以很好的出来android开发过程中,由bitmap导致的内存溢出的问题。
使用BitmapFactory加载图片时,如果图片过大可能引起OutOfmemory溢出的问题。解决办法。
关于android内存机制的介绍,以及bitmap的内存泄露等问题的处理方式
Android不支持将Bitmap转换成单色的Bmp图片,所以参考Bmp格式说明,自己写了一个转换类。亲测有效!!!
android中对Bitmap图片设置任意角为圆角
Android图片Bitmap和字符串String之间相互转换的工具类,外加项目代码,封装好的,直接可以用,
Android 内存 OOM 优化和治理,Android 内存泄漏原因详解与应对措施,Android 中的 Bitmap 使用详解,Android 中的 Bitmap 内存治理和优化详解,Android 内存分析命令详解,Java内存模型。Android 内存 OOM 优化和...
android加载大图片的时候防止内存溢出的分析
主要介绍了 Android canvas drawBitmap方法详解及实例的相关资料,需要的朋友可以参考下
在将图片转换成Bitmap的时候,由于图片的大小不一样,当遇到很大的图片的时候会出现超出内存的问题,为了解决这个问题Android API提供了BitmapFactory.Options这个类. 2. 由于Android对图片使用内存有限制,若是...
Android bitmap Android Bitmap相关
主要介绍了Android 实现把bitmap图片的某一部分的颜色改成其他颜色,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
Android上解析24位深度Bitmap文件示例