码迷,mamicode.com
首页 > 其他好文 > 详细

Shape path too large to be rendered into a texture

时间:2014-10-21 21:09:24      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:android   http   io   os   ar   使用   for   sp   数据   

Shape path too large to be rendered into a texture (747x8294, max=8192x8192)

 

图像显示的时候出现该问题,经分析是由于开启硬件加速导致加载的path大小被限制。

 

问题出现场景:

从相册或者本地选择图片,在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

Shape path too large to be rendered into a texture

标签:android   http   io   os   ar   使用   for   sp   数据   

原文地址:http://www.cnblogs.com/cratos/p/4041188.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!