android 中解决gridview 等加载大图片出现oom的问题
来源:互联网 发布:星际淘宝网txt全文下载 编辑:程序博客网 时间:2024/05/10 09:14
问题出现场景:
从相册或者本地选择图片,在gridView中展示。别的页面中可以使用类似图片浏览工具查看所有展示的图片。
解决办法:使用BitmapFactory.Options这个类。
B:Bitmap too large to be uploaded into a texture (2448x3264, max=2048x2048)
问题:硬件加速对图片的大小有限制。
办法:<application android:hardwareAccelerated="false" ...>禁止硬件加速(这是所谓的简单粗暴的方法,我承认我无耻了)
比较好的解决方法是类似google map的实现:将图片分成不同的块,每次加载需要的块。android提供了一个方法:
http://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html
从相册或者本地选择图片,在gridView中展示。别的页面中可以使用类似图片浏览工具查看所有展示的图片。
当我的图片浏览页面中,加载所有的图片,当加载到大图片时候,出现OOM和 Bitmap too large to be uploaded into a texture (2448x3264, max=2048x2048)。
A、OOM:
现场:BitmapFactory.decodeFile(path);当通过一个Uri去构造一个Bitmap时,图片过大了。
一开始,我的处理方式:
// 获取这个图片的宽和高 int width = bm.getWidth(); int height = bm.getHeight(); // 定义预转换成的图片的宽度和高度 int newWidth = 400; int newHeight = 400; // 计算缩放率,新尺寸除原始尺寸 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 创建操作图片用的matrix对象 Matrix matrix = new Matrix(); // 缩放图片动作 matrix.postScale(scaleWidth, scaleHeight); // 创建新的图片 Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true); return bm;这样需要事先获取Bitmap的宽高,那么首先需要构造一个Bitmap,而构造的时候,由于图片过大,出现OOM。
解决办法:使用BitmapFactory.Options这个类。
options有一个属性:boolean android.graphics.BitmapFactory.Options.inJustDecodeBounds options.inJustDecodeBounds = true; bm = BitmapFactory.decodeFile(path, options);/* If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.如果该属性设置为true,解析器将不会返回Bitmap,而是null。但是options.outWidth ,options.outHeight 的字段仍然被设置,方便调用者查询Bitmap的宽高。不需要为Bitmap的实际像素划分内存。*/拿到Bitmap的宽高后,设置为自己想要的值后,重新构造自己的Bitmap:
options.outWidth = 400; options.outHeight = 400; options.inJustDecodeBounds = false; bm = BitmapFactory.decodeFile(path, options);*******************************************上述方法可以得到满意的图片,但是Factory在decode的时候,仍然没有节约内存,使用下面三个属性,真正节约手机有限的内存: options.inPreferredConfig = Bitmap.Config.RGB_565;//默认为ARGB_8888. //以下两个字段需一起使用: options.inPurgeable = true;//产生的位图将得到像素空间,如果系统gc,那么将被清空。当像素再次被访问,如果Bitmap已经decode,那么将被自动重新解码 options.inInputShareable = true;//位图可以共享一个参考输入数据(inputstream、阵列等) options.inSampleSize = 4;//decode 原图1/4
B:Bitmap too large to be uploaded into a texture (2448x3264, max=2048x2048)
问题:硬件加速对图片的大小有限制。
办法:<application android:hardwareAccelerated="false" ...>禁止硬件加速(这是所谓的简单粗暴的方法,我承认我无耻了)
比较好的解决方法是类似google map的实现:将图片分成不同的块,每次加载需要的块。android提供了一个方法:
http://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html
0 0
- android 中解决gridview 等加载大图片出现oom的问题
- android中setImageResource加载大图片出现oom解决方案
- android中setImageResource加载大图片出现oom解决方案
- Android中setImageResource加载大图片出现oom解决方案
- android中setImageResource加载大图片出现oom解决方案
- android中setImageResource加载大图片出现oom解决方案
- android中setImageResource加载大图片出现oom解决方案
- Android加载大图片OOM异常解决
- Android加载大图片OOM异常解决
- android显示加载大图片OOM问题
- 【OOM】Android加载大图片OOM异常解决
- 加载大图片OOM问题
- Android 加载大图片是出现的 OutOfMemoryError问题
- 解决Android大图片加载问题
- Android中加载大图片OOM(图片缩放)
- Android中加载大图片放置OOM异常
- BitmapFactory.Options 解决加载大图片OOM
- 解决加载大图片oom处理
- 两语句功能相同,为何结果不相同?
- pthread_create用法
- 并行支持向量机算法的知识点之乔里斯基分解(Cholesky factorization)
- java基础24_多线程
- 人生就像饺子
- android 中解决gridview 等加载大图片出现oom的问题
- 模块化手机Project Ara的第一次开发者大会,有感
- 八数码
- 制作自定义CSDNBLOG皮肤
- [转载]互斥锁
- NSURL的scheme, path和query等各接口输出结果
- 键盘监控的实现Ⅰ——Keyboard Hook API函数
- gitweb setup base on git repo
- JAVA 运行 提示 Exception in thread "main" java.lang.NoClassDefFoundError 解决方法