第一个问题是out of memory

java.lang.OutOfMemoryError: bitmap size exceeds VM budget


1. 从源文件生成图片时,直接将图片缩小,而不是加载原始大小的图片。如下代码:

            Bitmap bitmap = null;
            if (mUseZoomOut || mUseZoomIn) {
                // decode image size (decode metadata only, not the whole image)
                o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                stream = new FileInputStream(filename);
                BitmapFactory.decodeStream(stream, null, o);

                // get original image size
                int inWidth =  o.outWidth;
                int inHeight = o.outHeight;
                clog(String.format("Original bitmap size: (%dx%d).", inWidth, inHeight));

                // get size for pre-resized image
                o = new Options();
                o.inSampleSize = Math.max(inWidth/targetWidth, inHeight/targetHeight);

            // decode pre-resized image
            stream = new FileInputStream(filename);
            // o.inPurgeable = true;
            bitmap = BitmapFactory.decodeStream(stream, null, o);
            clog(String.format("Pre-sized bitmap size: (%dx%d).", bitmap.getWidth(), bitmap.getHeight()));

2. 及时删除不需要使用的bitmap对象,不要将所有的对象都cache住

3. 增加程序的heap size。从某个版本开始,android manifest文件里有一个新的属性了:



Whether your application's processes should be created with a large Dalvik heap. This applies to all processes created for the application. It only applies to the first application loaded into a process; if you're using a shared user ID to allow multiple applications to use a process, they all must use this option consistently or they will have unpredictable results.
Most apps should not need this and should instead focus on reducing their overall memory usage for improved performance. Enabling this also does not guarantee a fixed increase in available memory, because some devices are constrained by their total available memory.
To query the available memory size at runtime, use the methods getMemoryClass() or getLargeMemoryClass().


Bitmap too large to be uploaded into a texture exception


简单说就是硬件加速的时候,对图片的大小有限制。不同设备可能有不同的最大值。这个问题悲催的地方是,程序貌似没有捕获到这个exception, 结果是程序也不报错,图片也显示不出来。只有看debug log才能发现这个error message.



Whether or not hardware-accelerated rendering should be enabled for all activities and views in this application — "true" if it should be enabled, and "false" if not. The default value is "true" if you've set either minSdkVersion or targetSdkVersion to "14" or higher; otherwise, it's "false".
Starting from Android 3.0 (API level 11), a hardware-accelerated OpenGL renderer is available to applications, to improve performance for many common 2D graphics operations. When the hardware-accelerated renderer is enabled, most operations in Canvas, Paint, Xfermode, ColorFilter, Shader, and Camera are accelerated. This results in smoother animations, smoother scrolling, and improved responsiveness overall, even for applications that do not explicitly make use the framework's OpenGL libraries.
Note that not all of the OpenGL 2D operations are accelerated. If you enable the hardware-accelerated renderer, test your application to ensure that it can make use of the renderer without errors.
For more information, read the Hardware Acceleration guide.

比较好的解决方法是类似google map的实现:将图片分成不同的块,每次加载需要的块。android提供了一个方法:

public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint)

public Bitmap decodeRegion (Rect rect, BitmapFactory.Options options)



这是一个自定义的DaweeView, 实现了2个功能:一个是可以使用wrap_content的方式指定fresco图片的大小(需要一个维度有确定值);第二个是可以显示大图:当图片的宽度太宽(大于屏幕宽度1.5倍)时,自动resize图片;当图片的高度太高(大于openGL能接受的最大图片高度,并且考虑图片可能会被放大的情况)时,自动把图片拆解成几个小的图片,从而避免硬件加速开启时的问题。


Print Friendly

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>