最近在做项目的时候遇到了这么个异常:
Bitmap too large to be uploaded into a texture
不难看出字面上的意思就是你这个Bitmap太大了,Android小妾做不到给你渲染上去哈。
我们来看看根本原因,由于项目的需要在Webview中来播放视频,所以在Activity层我开启了硬件加速,这带来了得问题就是我的Bitmap的图层超出了GPU对于openglRender的限制值,这个不同的手机会有不同的限制。
上网查资料发现有的朋友说,这个限制值可以通过canvas.getMaximumBitmapHeight()和canvas.getMaximumBitmapWidth()来获得。
但是我发现这种方式并没有拿到真正的限制值。
首先来给大家看看我的异常信息:
08-17 15:41:02.815: W/OpenGLRenderer(19967): Bitmap too large to be uploaded into a texture (2160x7332, max=4096x4096)
不难看出我的限制值是4096X4096,但是我通过canvas.getMaximumBitmapHeight()和canvas.getMaximumBitmapWidth()这两个方法拿到的限制值是三万多,显然不正确。
后来查资料,我的解决方案如下:
第一步:拿到本设备的GPU对于openglRender的限制值
//added by Jack for handle exception "Bitmap too large to be uploaded into a texture". public boolean isNeedCloseHardwareAcceleration(int w, int h) { int[] maxSize = new int[1]; GLES10.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxSize, 0); if(maxSize[0] < h || maxSize[0] < w) { return true; } return false; }
第二步:在你初始化视图的时候判断一下你的Bitmap的长宽是否超出了这个值,选择关闭硬件加速。当然你也可以分片来绘图。
至于如何开启、关闭、判断是否有硬件加速大家可以看看以下博文,这里我就不做介绍了。
http://www.cnblogs.com/frydsh/archive/2012/10/23/2733581.html
There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.