android 中解决gridview 等加载大图片出现oom的问题

问题出现场景:
从相册或者本地选择图片,在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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值