Bitmap导致的OOM 一些解决方案
2015-06-08 11:23
447 查看
(http://www.eoeandroid.com/thread-166226-1-1.html) 相机越来越好,相片也越来越大, 而手机应用程序所分配的内存有限, 所以在读相片的时候,如果代码写得不好,经常导致OOM. 信息如下: java.lang.OutOfMemoryError: bitmap size exceeds VM budget 基本上要注意几个地方: 1 bitmap如果不用了,回收掉 protected void onDestroy() { super.onDestroy(); if(bmp1 != null){ bmp1.recycle(); bmp1 = null; } if(bmp2 != null){ bmp2.recycle(); bmp2 = null; } } 2 先算出该bitmap的大小,然后通过调节采样率的方式来规避 BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeFile(imageFile, opts); opts.inSampleSize = computeSampleSize(opts, minSideLength, maxNumOfPixels); opts.inJustDecodeBounds = false; try { return BitmapFactory.decodeFile(imageFile, opts); } catch (OutOfMemoryError err) { } return null; 3 在进行文件传输时,最好采用压缩的方式变成byte[]再传输 public static byte[] bitmap2Bytes(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 90, baos); return baos.toByteArray(); } 可运行文件查看附件 由dxamhm补充了三个: 图片不用即时回收,不用等到destory,软引用,jni等 |
![](http://www.eoeandroid.com/static/image/filetype/rar.gif)
Demo_BitmapOOM_Solution.rar
2012-1-11 10:33 上传
点击文件名下载附件
下载积分: e币 -2 元
57.68 KB, 下载次数: 274, 下载积分: e币 -2 元
相关文章推荐
- XenServer重置存储管理SR至Ext3
- @RenderBody、@RenderSection、@RenderPage、Html.RenderPartial、Html.RenderAction的作用和区别
- Suse11sp3安装Oracle 11gR2
- photoshop基础 读书笔记
- 将数据表的纵向数据横向显示
- 十个JavaScript中易犯的小错误,你中了几枪?
- 1、我的第一个Django页面
- 豆瓣首页模仿
- 数据结构和算法学习二,之循环和递归
- iOS的主要框架介绍
- 愚公移山WP版强势来袭
- SQL 查询横表变竖表
- C#基础-----复杂数据类型
- WPF ResourceDictionary的使用
- 移动端 触屏轮播图(完善版)
- 简介JavaScript中strike()方法的使用
- ajax同步与异步理解
- Struts2项目配置
- SAT官方每日一题语法题汇总3
- Android 列表滚动