如何使用大量位图的工作,并避免错误:"位图的大小超过VM预算"? [英] How to work with a lot of bitmaps and avoid the error: "bitmap size exceeds VM budget"?
问题描述
可能重复:结果
安卓怪掉的内存问题的
我试图用1024×1024的位图4的工作,我需要在内存中四条,因为我创建画廊必须显示四个可缩放的位图以横向模式。
I'm trying to work with four bitmaps of 1024 x 1024, I need to have four in memory because I am creating a gallery that must show four zoomable bitmaps in landscape mode.
据工作摩托罗拉Droid 2.2.3,但它是失败的星系角宿一(2.1)。这是我得到斯皮卡异常:
It is working on Motorola Droid 2.2.3, but it is failing on galaxy spica (2.1). This is the exception that i get on spica:
02-28 15:45:25.963: ERROR/dalvikvm-heap(17109): 4194304-byte external allocation too large for this process.
02-28 15:45:25.968: ERROR/(17109): VM won't let us allocate 4194304 bytes
02-28 15:45:25.973: DEBUG/AndroidRuntime(17109): Shutting down VM
02-28 15:45:25.973: WARN/dalvikvm(17109): threadid=3: thread exiting with uncaught exception (group=0x4001b180)
02-28 15:45:25.973: ERROR/AndroidRuntime(17109): Uncaught handler: thread main exiting due to uncaught exception
02-28 15:45:25.988: ERROR/AndroidRuntime(17109): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
如何解决此问题?
how to solve this?
感谢
推荐答案
您可以通过使用RGB_565与默认ARGB_8888削减了一半大小。
You can cut the size in half by using RGB_565 versus the default ARGB_8888.
-
ARGB_8888要求每像素4个字节:4 * 1024 * 1024 = 4194304(4MB)
ARGB_8888 requires 4 bytes per pixel: 4*1024*1024 = 4194304 (4mb)
RGB_565要求每个像素2个字节:2 * 1024 * 1024 = 2097152(2MB)
RGB_565 require 2 bytes per pixel: 2*1024*1024 = 2097152 (2mb)
您可以选择到 BitmapFactory
设置此。见<一href=\"http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#in$p$pferredConfig\"相对=nofollow>在preferredConfig
You can set this with options to the BitmapFactory
. See inPreferredConfig
然而,即使这样,你还是会遇到困难。为什么不削减图像的大小下来呢?我怀疑你能无论如何显示1024×1024的图像。
However even with this you'll still have difficulty. Why not cut the size of the images down as well? I doubt you're able to show a 1024x1024 image anyway.
这篇关于如何使用大量位图的工作,并避免错误:&QUOT;位图的大小超过VM预算&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!