您的位置:首页 > 移动开发 > Android开发

android 读取本地大图片时防止oom方法

2015-08-05 16:00 495 查看
android 在读取本图片时,会消耗大量内存,图片一旦太大,就会报OUT OF MEMORY,常用一种解决方法:即将载入的图片缩小,这种方式以牺牲图片的质量为代价。在BitmapFactory中有一个内部类BitmapFactory.Options

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

现在问题是怎么确定inSampleSize的值?每张图片的放缩大小的比例应该是不一样的!这样的话就要运行时动态确定。在BitmapFactory.Options中提供了另一个成员inJustDecodeBounds。

设置inJustDecodeBounds为true后,decodeFile并不分配空间,但可计算出原始图片的长度和宽度,即opts.width和opts.height。有了这两个参数,再通过一定的算法,即可得到一个恰当的inSampleSize。Android提供了一种动态计算的方法,见computeSampleSize().代码如下:

public static Bitmap getBitmapFromFile(File dst, int width, int height) {

if (null != dst && dst.exists()) {

BitmapFactory.Options opts = null;

if (width > 0 && height > 0) {

opts = new BitmapFactory.Options(); //设置inJustDecodeBounds为true后,decodeFile并不分配空间,此时计算原始图片的长度和宽度

opts.inJustDecodeBounds = true;

BitmapFactory.decodeFile(dst.getPath(), opts);

// 计算图片缩放比例

final int minSideLength = Math.min(width, height);

opts.inSampleSize = computeSampleSize(opts, minSideLength,

width * height); //这里一定要将其设置回false,因为之前我们将其设置成了true

opts.inJustDecodeBounds = false;

opts.inInputShareable = true;

opts.inPurgeable = true;

}

try {

return BitmapFactory.decodeFile(dst.getPath(), opts);

} catch (OutOfMemoryError e) {

e.printStackTrace();

}

}

return null;

}

调用computeSampleSize()方法;

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;

}

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;

}

}


1 用decodeFileDescriptor()来生成bimap比decodeFile()省内存

用

FileInputStream is = = new FileInputStream(path);

bmp = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts);

替换掉

Bitmap bmp = BitmapFactory.decodeFile(imageFile, opts);

imageView.setImageBitmap(bmp);

原因:

查看BitmapFactory的源码,对比一下两者的实现,可以发现decodeFile()最终是以流的方式生成bitmap 

decodeFileDescriptor的源码,可以找到native本地方法decodeFileDescriptor,通过底层生成bitmap
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: