在做图片处理的时候最常遇到的问题估计就是Out Of Memory (内存溢出)了
网上对这种问题的解决方案很多,原来无非就是压缩图片大小
本不该重复造轮子,但实际中却遇见了问题,写出来希望后来者能引以为戒,并给出一个自我感觉不错的方案
常用的一种解决方案:
FileInputStream f = new FileInputStream(file);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;//将图片大小改为原来的1/4
Bitmap bm = BitmapFactory.decodeStream(f, null, options);
其中options.inSampleSize定义如下:
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 <= 1 is treated the same as 1. Note: the decoder will try to fulfill this request, but the resulting bitmap may have different dimensions that precisely what has been requested. Also, powers of 2 are often faster/easier for the decoder to honor.
也就是说options.inSampleSize = 2就是将原图片的高和宽都设为1/2(单位为像素),整体图片也就缩小成原来的1/4之一了
这当然是一种很好的解决方案,只需根据自己的需要设置缩放比就行。
但这仅仅解决了一部分问题
试想一下,如果每张图片的大小都不一样,而要显示在相同的控件中,这时的缩放比(options.inSampleSize)该设置成多少呢?
如果设置缩放基本不够小,对于大像素图片的处理依然会出现内存溢出,设置得太大又会导致图片失真
这真是个头疼的问题。
如果能够根据图片大小自动计算缩放比那该多好啊!
别急,google已经为我们提供了这么一个方法(出自源码 暂且保存为BitmapUtiles.java 方便以后使用):
public class BitmapUtils
{
public static int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels);
int roundedSize;
if (initialSize <=
{
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
private static int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
}
我们只需要这样使用就好了:
FileInputStream f = new FileInputStream(file);
FileDescriptor fd = f.getFD();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fd, null, options);
options.inSampleSize = BitmapUtils.computeSampleSize(options, 80, 128*128);
options.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeStream(f, null, options);
其中需要注意的是options.inJustDecodeBounds这个属性,API解释如下:
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.
在设置为true的情况下不会返回bitmap,即不会占用内存,但可以计算bitmap的size大小(BitmapFactory.decodeFileDescriptor(fd, null, options) 由options带回宽、高)
此时的操作就是为了得到高、宽用于后面缩放比的计算
BitmapUtils.computeSampleSize(options, 80, 128*128)中
80是最小边长,128*128是期望得到的图像像素
特别注意的是,计算完宽高后一定要将改属性设置为false,否则BitmapFactory.decodeStream会一直返回null
OK,讲得够详细了
转自:
http://blog.csdn.net/lovehong0306/article/details/7741436#comments
分享到:
相关推荐
android bitmap outofMemory 用来解决android中常见的bitmap outOfMemory
ANDROIDBITMAP内存限制OOM,OUTOFMEMORY.pdf
处理bitmap内存溢出问题
ANDROIDBITMAP内存限制OOM,OUTOFMEMORY[文].pdf
使用BitmapFactory加载图片时,如果图片过大可能引起OutOfmemory溢出的问题。解决办法。
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts); 设置inJustDecodeBounds为true后,decodeFile并不分配空间,但可计算出原始图片的长度和宽度,即opts.width和opts.height。有了这两个参数,再通过...
可以很好的出来android开发过程中,由bitmap导致的内存溢出的问题。
改用原始图像数据流回调函数RegisterImageStreamCallback进行抓图 但回调函数帧为YUV420格式 需要转成Bitmap才用用于显示及本地查看 在网上找了好多都是基于R Y+1 4075 V 128 G Y 0 3455 U 128 – 0 7169 V 128 B Y+...
Android中bitmap引起内存溢出的解决方案的对比分析.pdf
android加载大图片的时候防止内存溢出的分析
Camera onPreview中byte[] 转换为Bitmap 在录像时设置了YV12要保存图片时 YV12或者NV12时要转换时所用的工具的优化
机器视觉技术必备,从位图到halcon图像,bitmap图像转为halcon图像,bitmap to Hobject
android里面 canvas 转换成bitmap
Halcon_Bitmap转换方法 写的文档 开发笔记
Android View转成Bitmap
博客配套文件,解决常见的Bitmap转HBITMAP背景变浅,HBITMAP转Bitmap背景变黑的问题,并演示了如何输出HBITMAP和Bitmap的每行像素,所有代码封装成了类,可以直接在生产环境中使用。
一个在VC中将Bitmap转换为Byte[]的小例子。
在将图片转换成Bitmap的时候,由于图片的大小不一样,当遇到很大的图片的时候会出现超出内存的问题,为了解决这个问题Android API提供了BitmapFactory.Options这个类. 2. 由于Android对图片使用内存有限制,若是...