Bitmap—— BitmapFactory.decodeFile

转自http://www.cnblogs.com/zgz345/archive/2013/01/08/2851204.html

android设备上(where you have only 16MB memory available),如果使用BitmapFactory解码一个较大文件,程序立刻蹦出了java.lang.OutOfMemoryError: bitmap size exceeds VM budget的OOM错误!


解决办法:

方法一:将载入的图片缩小,这种方式以牺牲图片的质量为代价。在BitmapFactory中有一个内部类BitmapFactory.Options,其中当options.inSampleSize值>1时,

根据文档:

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.

也就是说,options.inSampleSize是以2的指数的倒数被进行放缩。这样,我们可以依靠inSampleSize的值的设定将图片放缩载入,这样一般情况也就不会出现上述的OOM问题了。现在问题是怎么确定inSampleSize的值?每张图片的放缩大小的比例应该是不一样的!这样的话就要运行时动态确定。在BitmapFactory.Options中提供了另一个成员inJustDecodeBounds。


BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);

设置inJustDecodeBounds为true后,decodeFile并不分配空间,但可计算出原始图片的长度和宽度,即opts.width和opts.height。有了这两个参数,再通过一定的算法,即可得到一个恰当的inSampleSize。

Android提供了一种动态计算的方法。如下:


public 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();<br><br>            //设置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);<br><br>            //这里一定要将其设置回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;
}

  
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;
    }
}


这样,在BitmapFactory.decodeFile执行处,也就不会报出上面的OOM Error。

如前面提到的,这种方式在一定程度上是以牺牲图片质量为代价的。如何才能更加优化的实现需求?

当在android设备中载入较大图片资源时,可以创建一些临时空间,将载入的资源载入到临时空间中。


BitmapFactory.Options bfOptions=new BitmapFactory.Options();
bfOptions.inTempStorage=newbyte[12 * 1024];

以上创建了一个12kb的临时空间。然后使用

Bitmap bitmapImage = BitmapFactory.decodeFile(path,bfOptions);

但是我在程序中却还是出现以上问题!以下使用BitmapFactory.decodeFileDescriptor解决了以上问题:

BitmapFactory.Options bfOptions=new BitmapFactory.Options(); 
bfOptions.inDither=false; 
bfOptions.inPurgeable=true; 
bfOptions.inTempStorage=newbyte[12 * 1024];  
bfOptions.inJustDecodeBounds = true; 
File file = new File(pePicFile.getAbsolutePath() + "/"+info.getImage()); 
FileInputStream fs=null; 
Bitmap bmp = null; 
try { 
    fs = new FileInputStream(file); 
    if(fs != null) 
        bmp = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions); 
     } catch (Exception e) { 
    e.printStackTrace(); 
}

当然要将取得图片进行放缩显示等处理也可以在以上得到的bmp进行。

图片处理后记得回收内存
if(!bmp.isRecycle() ){
   bmp.recycle()   //回收图片所占的内存         
   system.gc()  //提醒系统及时回收
}



  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用和引用的内容,可以使用以下方式引用BitmapFactory.decodeFile: ```java BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts); ``` 其中,`BitmapFactory.Options`提供了一个成员`inJustDecodeBounds`,将其设置为`true`可以只解析图片的边界信息而不加载整个图片,这样可以避免内存溢出的问题。然后,可以根据需要对`BitmapFactory.Options`的其他成员进行设置,如`inSampleSize`来降低图片的采样率以减少内存使用。最后,使用`BitmapFactory.decodeFile`方法来解码指定路径的图片文件并返回一个`Bitmap`对象。 另外,根据引用的内容,还可以通过打印出图片路径来确认是否有权限访问该路径的图片。 请注意,以上内容仅为引用内容的解释和引用方法的说明,具体使用时还需要根据实际情况进行适当修改和调整。123 #### 引用[.reference_title] - *1* *2* [在使用BitmapFactory.decodeFile时出现java.lang.OutOfMemoryError](https://blog.csdn.net/a518618718/article/details/127817955)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *3* [BitmapFactory.decodeFile总返回null的解决方法](https://blog.csdn.net/wys_yuan/article/details/113943217)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值