转自http://www.maxhis.info/androiding/bitmap-size-exceed/

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

Java代码  
  1. BitmapFactory.Options opts = new BitmapFactory.Options();
  2. opts.inSampleSize = 4;
  3. Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);

如何设置恰当的inSampleSize
设置恰当的inSampleSize是解决该问题的关键之一。BitmapFactory.Options提供了另一个成员inJustDecodeBounds。

Java代码  
  1. BitmapFactory.Options opts = new BitmapFactory.Options();
  2. opts.inJustDecodeBounds = true;
  3. Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);

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

Java代码  
  1. public static int computeSampleSize(BitmapFactory.Options options,
  2. int minSideLength, int maxNumOfPixels) {
  3. int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);
  4. int roundedSize;
  5. if (initialSize <= 8 ) {
  6. roundedSize = 1;
  7. while (roundedSize < initialSize) {
  8. roundedSize <<= 1;
  9. }
  10. } else {
  11. roundedSize = (initialSize + 7) / 8 * 8;
  12. }
  13. return roundedSize;
  14. }
  15. private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
  16. double w = options.outWidth;
  17. double h = options.outHeight;
  18. int lowerBound = (maxNumOfPixels == -1) ? 1 :
  19. (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
  20. int upperBound = (minSideLength == -1) ? 128 :
  21. (int) Math.min(Math.floor(w / minSideLength),
  22. Math.floor(h / minSideLength));
  23. if (upperBound < lowerBound) {
  24. // return the larger one when there is no overlapping zone.
  25. return lowerBound;
  26. }
  27. if ((maxNumOfPixels == -1) &&
  28. (minSideLength == -1)) {
  29. return 1;
  30. } else if (minSideLength == -1) {
  31. return lowerBound;
  32. } else {
  33. return upperBound;
  34. }
  35. }

使用该算法,就可动态计算出图片的inSampleSize。

Java代码  
  1. BitmapFactory.Options opts = new BitmapFactory.Options();
  2. opts.inJustDecodeBounds = true;
  3. BitmapFactory.decodeFile(imageFile, opts);
  4. opts.inSampleSize = computeSampleSize(opts, -1, 128*128);
  5. opts.inJustDecodeBounds = false;
  6. try {
  7. Bitmap bmp = BitmapFactory.decodeFile(imageFile, opts);
  8. imageView.setImageBitmap(bmp);
  9. } catch (OutOfMemoryError err) {
  10. }

另外,可以通过

Java代码  
  1. Bitmap.recycle()

方法来释放位图所占的空间,当然前提是位图没有被使用。

转载于:https://www.cnblogs.com/zsw-1993/archive/2012/10/17/4880796.html

完美解决java.lang.OutOfMemoryError: bitmap size exceeds VM budget相关推荐

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

    当图片过大,或图片数量较多时使用BitmapFactory解码图片会出java.lang.OutOfMemoryError: bitmap size exceeds VM budget,要想正常使用则 ...

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

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

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

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

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

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

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

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

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

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

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

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

  8. 【android错误】bitmap size exceeds 32bits

    为什么80%的码农都做不了架构师?>>>    使用图片缩放时遇到这么个问题: java.lang.IllegalArgumentException: bitmap size exc ...

  9. Java 内存溢出(java.lang.OutOfMemoryError)解决

    导致OutOfMemoryError异常的常见原因有以下几种: 内存中加载的数据量过于庞大,如一次从数据库取出过多数据: 集合类中有对对象的引用,使用完后未清空,使得JVM不能回收: 代码中存在死循环 ...

最新文章

  1. 【笔记】PIL 中的 Image 模块
  2. 用python画qq表情_用Python编写提取QQ表情的脚本
  3. 使用ueditor实现多图片上传案例——Service层(IShoppingService)
  4. Keras-7 Reuters, a multiclass classification example
  5. 计算机在矿山企业中的应用,计算机在矿山工业中的应用与发展
  6. innodb_file_per_table 理解
  7. 深度学习2.0-20.Keras高层API-metrics
  8. v8go 库手动编译 v8 golang 库手动编译
  9. 手机IMSI号码编码规则表
  10. ftp服务器FileZilla Server详细配置教程
  11. TCON控制字及TMOD寄存器
  12. Ext:Panel之applyTo与renderTo区别
  13. 【持续更新】2000-2022年英伟达历代桌面Quadro显卡列表,Quadro显卡发布日期
  14. xp设置允许客户端远程连接_远程删除Windows XP客户端中的用户配置文件
  15. 黑马5月就业数据丨人均过万!女生薪资更亮眼!
  16. godot引擎学习6
  17. Rust图片类型识别
  18. 本题要求提取一个字符串中的所有数字字符(‘0‘……‘9‘),将其转换为一个整数输出。
  19. uni-app生成分享图片( 使用 Painter 生成分享海报)
  20. 穷苦人民如何用移动固态配置unbutu22.04

热门文章

  1. 【Oracle 数据迁移】环境oracle 11gR2,exp无法导出空表的表结构【转载】
  2. 网站部署后Parser Error Message: Could not load type 的解决方案
  3. I.MX6 Linux Serial Baud Rate hacking
  4. Linux 引导过程精讲
  5. Using dispatch_async
  6. IPSEC---动态MAP-VS-静态MAP
  7. 软件测试 学习之路 linux vim编辑器
  8. sysadmin默认密码_从sysadmin过渡到DevOps工程师的案例
  9. Struts Action 控制器
  10. 脚本化HTTP 取得响应 指定请求