当图片过大,或图片数量较多时使用BitmapFactory解码图片会出java.lang.OutOfMemoryError: bitmap size exceeds VM budget,要想正常使用则需分配更少的内存,具体的解决办法是修改采样值BitmapFactory.Options.inSampleSize,例如:

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

如何设置恰当的inSampleSize

设置恰当的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()方法来释放位图所占的空间,当然前提是位图没有被使用。

转载于:https://www.cnblogs.com/xiao0/archive/2011/09/13/2174399.html

转载 Android解决java.lang.OutOfMemoryError: bitmap size exceeds VM budget相关推荐

  1. Android解决java.lang.OutOfMemoryError: bitmap size exceeds VM budget(转)

    昨天遇到这个问题就是从一个输入流里调用BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri))得到一个bit ...

  2. Android解决java.lang.OutOfMemoryError: bitmap size exceeds VM budget

    昨天遇到这个问题就是从一个输入流里调用BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri))得到一个bit ...

  3. 完美解决java.lang.OutOfMemoryError: bitmap size exceeds VM budget

    转自http://www.maxhis.info/androiding/bitmap-size-exceed/ 当图片过大,或图片数量较多时使用BitmapFactory解码图片会出java.lang ...

  4. android报错及解决1--Bitmap加载时,报bitmap size exceeds VM budget

    报错描述: 用Bitmap加载图片资源时,报错java.lang.OutOfMemoryError: bitmap size exceeds VM budget 原因分析: android系统限制,只 ...

  5. 解决“java.lang.OutOfMemoryError: Failed to allocate a allocation until OOM”错误

    1.参考:解决"java.lang.OutOfMemoryError: Failed to allocate a allocation until OOM"错误_SEVENY_的博 ...

  6. 解决java.lang.OutOfMemoryError: unable to create new native thread问题

    解决java.lang.OutOfMemoryError: unable to create new native thread问题 参考文章: (1)解决java.lang.OutOfMemoryE ...

  7. Android之java.lang.OutOfMemoryError: Failed to allocate a ** byte allocation with **free bytes and 2M

    1 问题 glide加载图片出现oom java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2 ...

  8. 解决 - java.lang.OutOfMemoryError: unable to create new native thread

    一.认识问题: 首先我们通过下面这个 测试程序 来认识这个问题: 运行的环境 (有必要说明一下,不同环境会有不同的结果):32位 Windows XP,Sun JDK 1.6.0_18, eclips ...

  9. Android解决java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag

    我们使用android https验证的的时候出现如下错误: 03-17 10:47:01.941: W/System.err(12702): java.security.cert.Certifica ...

最新文章

  1. 美国科学院2020新晋院士名单发布!中科院曹晓风及6位华人学者入选,去年还有高福和颜宁...
  2. ASP.NET Web 表单
  3. 【C++】构建栈 进栈和出栈
  4. 深度学习与计算机视觉(二)线性SVM与Softmax分类器
  5. php curl for win7_win7 wamp 64位 php环境开启curl服务遇到的问题及解决方法
  6. 对《技术人员,你拿什么拯救你的生活----温水煮青蛙》的一点看法
  7. 在windows系统上安装pip的注意事项
  8. linux下安装卸载永中office步骤,永中集成Office For Linux安装图文指南及简介
  9. 《大学之路》读书笔记(上)范文3700字
  10. 32位服务器系统支持8G内存,32位系统怎么支持8g内存条win10 64位系统闲置服务器...
  11. 记录使用itextpdf通过定位插入图片和文字
  12. 使用Axis2创建一个Web Service的客户端 - 子非鱼,安知鱼之乐? - CSDNBlog
  13. Python编程:根据经纬度生成并调用地图
  14. 执行python manage.py migrate报错问题解决
  15. 2020.5.31 牛客“科林明伦杯” A.点对最大值【树形dp】
  16. Big Faceless Java PDF Library[BFO]
  17. echarts设置坐标轴标题的样式
  18. 如何利用Qt 3D 渲染与 Qt Quick 2D 元素结合创建太阳系行星元素?
  19. 分镜头脚本表格模板下载
  20. win10上部署elasticsearch8.1.1

热门文章

  1. 手机知识:手机快充取决于充电头还是数据线,看完你就懂了!
  2. Linux排序命令sort笔记
  3. Android 第七课 4种基本布局之FrameLayout和百分比布局
  4. 和菜鸟一起学linux之DBUS基础学习记录
  5. 最新ui设计趋势_10个最新且有希望的UI设计趋势
  6. sketch浮动布局_使用智能布局和调整大小在Sketch中创建更好的可重用符号
  7. Gitee 如何自动部署博客 Pages?推荐用这个GitHub Actions!
  8. 上传文件的input问题以及FormData特性
  9. 基于http协议的api接口对于客户端的身份认证方式以及安全措施[转]
  10. HBase键值分片的简单运用