Android 优化Bitmap避免OOM

本文介绍了一种在Android应用中优化图片加载的方法,通过调整BitmapFactory.Options的参数避免内存溢出错误,同时保持较好的图片质量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用android提供的BitmapFactory解码图片时,往往会因为图片过大而遇到OutOfMemoryError的异常。要想正常使用,一种简便的方式是分配更少的内存空间来存储,即在载入图片的时候以牺牲图片质量为代价,将图片进行放缩,这是一种避免OOM所采用的解决方法。但是,这种方法是得不偿失的,牺牲了图片质量。

在BitmapFactory中有一个内部类BitmapFactory.Options,其中值得我们注意的是inSampleSize和inJustDecodeBounds两个属性:

    inSampleSize是以2的指数的倒数被进行放缩

inJustDecodeBounds为Boolean型

设置inJustDecodeBounds为true后,decodeFile并不分配空间,但可计算出原始图片的长度和宽度,即options.outWidth和options.outHeight。

要对图片进行缩放,最大的问题就是怎么在运行时动态的改变inSampleSize的值,通过上面的inJustDecodeBounds可以知道图片原始的大小,那么这样以来就可以通过算法来得到一个恰当的inSampleSize值。其动态算法可参考下面的,网上也很多,大体都一样:

/**
 * compute Sample Size
 * 
 * @param options
 * @param minSideLength
 * @param maxNumOfPixels
 * @return
 */
public static int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels);


int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}


return roundedSize;
}


/**
 * compute Initial Sample Size
 * 
 * @param options
 * @param minSideLength
 * @param maxNumOfPixels
 * @return
 */
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;
}
}

有了上面的算法,我们就可以轻易的get到Bitmap了:

/**
 * get Bitmap
 * 
 * @param imgFile
 * @param minSideLength
 * @param maxNumOfPixels
 * @return
 */
public static Bitmap tryGetBitmap(String imgFile, int minSideLength,
int maxNumOfPixels) {
if (imgFile == null || imgFile.length() == 0)
return null;


try {
FileDescriptor fd = new FileInputStream(imgFile).getFD();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// BitmapFactory.decodeFile(imgFile, options);
BitmapFactory.decodeFileDescriptor(fd, null, options);


options.inSampleSize = computeSampleSize(options, minSideLength,
maxNumOfPixels);
try {
// 这里一定要将其设置回false,因为之前我们将其设置成了true
// 设置inJustDecodeBounds为true后,decodeFile并不分配空间,即,BitmapFactory解码出来的Bitmap为Null,但可计算出原始图片的长度和宽度
options.inJustDecodeBounds = false;


Bitmap bmp = BitmapFactory.decodeFile(imgFile, options);
return bmp == null ? null : bmp;
} catch (OutOfMemoryError err) {
return null;
}
} catch (Exception e) {
return null;
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值