`
jiguansheng
  • 浏览: 125871 次
  • 性别: Icon_minigender_1
  • 来自: 九江
社区版块
存档分类
最新评论

错误:bitmap size exceeds VM budget

 
阅读更多

 

private Bitmap decodeFile(File f){
    Bitmap b = null;
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);
        int scale = 1;
        if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
            scale = Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        b = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return b;
}
 

 

转自http://www.cnblogs.com/RayLee/archive/2010/11/09/1872856.html

 

BitmapFactory.decodeFile(imageFile);

用BitmapFactory解码一张图片时,有时会遇到该错误。这往往是由于图片过大造成的。要想正常使用,则需要分配更少的内存空间来存储。

BitmapFactory.Options.inSampleSize

设置恰当的inSampleSize可以使BitmapFactory分配更少的空间以消除该错误。inSampleSize的具体含义请参考SDK文档。例如:

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);

 

 

设置恰当的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源码,Android提供了一种动态计算的方法。

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;
}
}	
使用该算法,就可动态计算出图片的inSampleSize。
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imageFile, opts);

opts.inSampleSize = computeSampleSize(opts, -1, 128*128);	
opts.inJustDecodeBounds = false;
try {
Bitmap bmp = BitmapFactory.decodeFile(imageFile, opts);
imageView.setImageBitmap(bmp);
} catch (OutOfMemoryError err) {
}

另外,可以通过Bitmap.recycle()方法来释放位图所占的空间,当然前提是位图没有被使用。

 

分享到:
评论

相关推荐

    android_内存溢出处理

    在 Android 中,用 bitmap 时很容易内存溢出,报如下错误:Java.lang.OutOfMemoryError:bitmap size exceeds VM budget。解决这个问题可以通过手动干涉 GC 去处理 bitmap 设置图片尺寸,避免内存溢出。 例如: ```...

    RoaringBitmap-0.7.45-API文档-中英对照版.zip

    Maven坐标:org.roaringbitmap:RoaringBitmap:0.7.45; 标签:roaringbitmap、RoaringBitmap、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 ...

    C# 图片裁剪器(使用:Bitmap)

    支持用户输入,裁剪切片的:尺寸大小,缩略图名。

    java算法:BitMap

    位图算法

    RoaringBitmap-0.7.45-API文档-中文版.zip

    Maven坐标:org.roaringbitmap:RoaringBitmap:0.7.45; 标签:roaringbitmap、RoaringBitmap、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化...

    位图与调色板VC源代码:bitmap_picture_src

    位图与调色板源代码:bitmap_picture_src 关键字:bitmap_picture_src,位图与调色板

    RoaringBitmap-0.5.11-API文档-中英对照版.zip

    Maven坐标:org.roaringbitmap:RoaringBitmap:0.5.11; 标签:roaringbitmap、RoaringBitmap、jar包、java、API文档、中英对照版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档...

    RoaringBitmap-0.5.11-API文档-中文版.zip

    Maven坐标:org.roaringbitmap:RoaringBitmap:0.5.11; 标签:roaringbitmap、RoaringBitmap、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化...

    对话框VC源代码:bitmap_preview_dialog_source

    对话框源代码:bitmap_preview_dialog_source 关键字:bitmap_preview_dialog_source,对话框

    Javascript的bitmap处理库jsBitmap.zip

    基于Javascript的bitmap处理,并且将位图输出为base64编码以便于浏览器进行显示。   一、Bitmap.create(width, height, bgcolor) 创建一个width x height像素大小的位图,底色为bgcolor所代表的颜色。 如:bitmap....

    Android安卓BitmMap工具类

    1.saveBitmap: 把Bitmap对象持久存储到SD卡或手机内存. 2.getViewBitmap: 从view得到bitmap对象 3.addWatermark: Bitmap加水印 4.zoomBitmap: 放大缩小图片 5.getLoacalBitmap: 传入路径,从持久存储(SD卡或手机内存)...

    VC中使用GDI+库,实现图片旋转

    基于VC++语言,VS平台开发,应用GDI+库,双缓冲,实现图片旋转,屏幕不闪烁。代码量不大,简单易学。

    Bitmap压缩原理和理论分析.jpg

    1:介绍图片耗用内存资源原理,以及计算图片内存方法 2:Bitmap压缩图片基本手段和思路

    GDI+动态库和头文件以及使用方法

    GDI+不但在功能上比GDI 要强大很多,而且在代码编写方面也更简单,因此会很快成为Windows图形图像程序开发的首选。

    libbmp:Bitmap Loader 库,基于代码共享站点的片段

    libbmp Bitmap Loader 库,基于代码共享站点的片段

    ico图标生成器源码

    ico图标生成器源码 ico图标生成器源码 ico图标生成器源码

    Delphi 图片颜色逐渐加深功能的实现.rar

     Bitmap:=image1.Picture.Bitmap;  image1.Height:= Bitmap.Height;  image1.Width:= Bitmap.Width;  for i:=0 to 255 do  begin  sleep(10);  for y:=0 to Bitmap.Height-1 do  begin  pixcolo:=Bitmap....

    vc GDI+实现屏幕截图,图片裁剪,

    VC2015实现简单的整个屏幕截图保存为jpg, 然后再该图片上根据需要裁剪特定大小的图片在另存jpg, 以及其他范例。

    Android中的Bitmap序列化失败的解决方法

    之前写了个User类(实现了Serializable接口),类变量里有Bitmap类型的头像图片,Bitmap导致序列化不成功,报 “android.graphics.Bitmap”相关错误 解决方法之一:把Bitmap对象替换成byte数组来表示间接表示图片,在...

    C++ 实现两个图片融合

    dwValue = bitmap-&gt;GetBitmapBits(bitmapSize1, px); bgbmp-&gt;GetBitmap(&bmpY;); bitmapSize = bmpY.bmHeight * bmpY.bmWidthBytes; BYTE* px1=(BYTE *)GlobalAlloc(GPTR,bitmapSize); dwValue2 = bgbmp-&gt;...

Global site tag (gtag.js) - Google Analytics