`
chjmars
  • 浏览: 75839 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Android 优化Bitmap避免OutOfMemoryError

阅读更多

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

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

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

If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. (1 -> decodes full size; 2 -> decodes 1/4th size; 4 -> decode 1/16th size). Because you rarely need to show and have full size bitmap images on your phone. For manipulations smaller sizes are usually enough.

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;
	}
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics