完美解决java.lang.OutOfMemoryError: bitmap size exceeds VM budget
当图片过大,或图片数量较多时使用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/zsw-1993/archive/2012/10/17/4880796.html
完美解决java.lang.OutOfMemoryError: bitmap size exceeds VM budget相关推荐
- 转载 Android解决java.lang.OutOfMemoryError: bitmap size exceeds VM budget
当图片过大,或图片数量较多时使用BitmapFactory解码图片会出java.lang.OutOfMemoryError: bitmap size exceeds VM budget,要想正常使用则 ...
- Android解决java.lang.OutOfMemoryError: bitmap size exceeds VM budget(转)
昨天遇到这个问题就是从一个输入流里调用BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri))得到一个bit ...
- Android解决java.lang.OutOfMemoryError: bitmap size exceeds VM budget
昨天遇到这个问题就是从一个输入流里调用BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri))得到一个bit ...
- android报错及解决1--Bitmap加载时,报bitmap size exceeds VM budget
报错描述: 用Bitmap加载图片资源时,报错java.lang.OutOfMemoryError: bitmap size exceeds VM budget 原因分析: android系统限制,只 ...
- 解决java.lang.OutOfMemoryError: unable to create new native thread问题
解决java.lang.OutOfMemoryError: unable to create new native thread问题 参考文章: (1)解决java.lang.OutOfMemoryE ...
- 解决“java.lang.OutOfMemoryError: Failed to allocate a allocation until OOM”错误
1.参考:解决"java.lang.OutOfMemoryError: Failed to allocate a allocation until OOM"错误_SEVENY_的博 ...
- 解决 - java.lang.OutOfMemoryError: unable to create new native thread
一.认识问题: 首先我们通过下面这个 测试程序 来认识这个问题: 运行的环境 (有必要说明一下,不同环境会有不同的结果):32位 Windows XP,Sun JDK 1.6.0_18, eclips ...
- 【android错误】bitmap size exceeds 32bits
为什么80%的码农都做不了架构师?>>> 使用图片缩放时遇到这么个问题: java.lang.IllegalArgumentException: bitmap size exc ...
- Java 内存溢出(java.lang.OutOfMemoryError)解决
导致OutOfMemoryError异常的常见原因有以下几种: 内存中加载的数据量过于庞大,如一次从数据库取出过多数据: 集合类中有对对象的引用,使用完后未清空,使得JVM不能回收: 代码中存在死循环 ...
最新文章
- 【笔记】PIL 中的 Image 模块
- 用python画qq表情_用Python编写提取QQ表情的脚本
- 使用ueditor实现多图片上传案例——Service层(IShoppingService)
- Keras-7 Reuters, a multiclass classification example
- 计算机在矿山企业中的应用,计算机在矿山工业中的应用与发展
- innodb_file_per_table 理解
- 深度学习2.0-20.Keras高层API-metrics
- v8go 库手动编译 v8 golang 库手动编译
- 手机IMSI号码编码规则表
- ftp服务器FileZilla Server详细配置教程
- TCON控制字及TMOD寄存器
- Ext:Panel之applyTo与renderTo区别
- 【持续更新】2000-2022年英伟达历代桌面Quadro显卡列表,Quadro显卡发布日期
- xp设置允许客户端远程连接_远程删除Windows XP客户端中的用户配置文件
- 黑马5月就业数据丨人均过万!女生薪资更亮眼!
- godot引擎学习6
- Rust图片类型识别
- 本题要求提取一个字符串中的所有数字字符(‘0‘……‘9‘),将其转换为一个整数输出。
- uni-app生成分享图片( 使用 Painter 生成分享海报)
- 穷苦人民如何用移动固态配置unbutu22.04
热门文章
- 【Oracle 数据迁移】环境oracle 11gR2,exp无法导出空表的表结构【转载】
- 网站部署后Parser Error Message: Could not load type 的解决方案
- I.MX6 Linux Serial Baud Rate hacking
- Linux 引导过程精讲
- Using dispatch_async
- IPSEC---动态MAP-VS-静态MAP
- 软件测试 学习之路 linux vim编辑器
- sysadmin默认密码_从sysadmin过渡到DevOps工程师的案例
- Struts Action 控制器
- 脚本化HTTP 取得响应 指定请求