Shape path too large to be rendered into a texture

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

时间: 2024-08-10 17:04:59

Shape path too large to be rendered into a texture的相关文章

Bitmap too large to be uploaded into a texture

从手机相册里面显示图片,但是发现有的图片能显示有的图片不能显示,路径都是对的,程序也没提示任何错误. 后来通过看日志发现error message: Bitmap too large to be uploaded into a texture (4208x3120, max=4096x4096): 经过查找资料是因为当开启硬件加速的时候,GPU对于openglRender 有一个限制,这个不同的手机会有不同的限制. 简单说就是硬件加速的时候,对图片的大小有限制.不同设备可能有不同的最大值.这个问

fresco Bitmap too large to be uploaded into a texture

fresco加载图片方法 布局文件引入 xmlns:fresco="http://schemas.android.com/apk/res-auto" <com.facebook.drawee.view.SimpleDraweeView android:id="@id/img_icon" android:layout_width="42.0dip" android:layout_height="42.0dip" andro

Bitmap too large to be uploaded into a texture exception

Bitmap too large to be uploaded into a texture exception 图片太大无法显示. 硬件加速中 OpenGL对于内存是有限制的 解决方式1: 禁用硬件加速 <application android:hardwareAccelerated="false" ...> 解决方式2:比较好的解决方法是类似google map的实现:将图片分成不同的块,每次加载需要的块.android提供了一个方法: http://developer

绘图: Shape, Path

Shape - 图形 Path - 路径 示例1.演示“Shape”相关知识点Drawing/Shape.xaml <Page x:Class="Windows10.Drawing.Shape" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xml

背水一战 Windows 10 (12) - 绘图: Shape, Path

[源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 绘图 Shape - 图形 Path - 路径 示例1.演示“Shape”相关知识点Drawing/Shape.xaml <Page x:Class="Windows10.Drawing.Shape" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schema

ZRender源码分析6:Shape对象详解之路径

开始 说到这里,就不得不提SVG的路径操作了,因为ZRender完全的模拟了SVG原生的path元素的用法,很是强大. 关于SVG的Path,请看这里: Path (英文版) 或者 [MDN]SVG教程(5) 路径 [译] (中文版), 很明显的是canvas中的路径没有SVG的用着舒服,那到底ZRender是如何实现的呢,让我给你娓娓道来(不过要想继续进行下去,上面的SVG的PATH必须了解.). 示例 打开API,shape.path,可以看到,path的配置有MLHVCSQTZ等字母组成的

C# Wpf Shape类继承关系

Path派生于Shape namespace System.Windows.Shapes { public sealed class Path : Shape { // Path 派生于Shape } } Shape 派生于FrameworkElement namespace System.Windows.Shapes { //Shape 从FrameworkElement派生而来 public abstract class Shape : FrameworkElement { } } Fram

Computer Graphics Research Software

Helping you avoid re-inventing the wheel since 2009! Last updated December 5, 2012.Try searching this page for keywords like 'segmentation' or 'PLY'.If you would like to contribute links, please e-mail them to [email protected]. Papers & Archives Gra

A trip through the Graphics Pipeline 2011_02

Welcome back. Last part was about vertex shaders, with some coverage of GPU shader units in general. Mostly, they’re just vector processors, but they have access to one resource that doesn’t exist in other vector architectures: Texture samplers. They