Android – why so much memory?
I have a 1000 × 1500 pixel bitmap, I want to make a variable copy in Android
When I run the following code
// int width = original.getWidth(); // 1000px
// int height = original.getHeight(); // 1500px
final Bitmap result = original.copy(original.getConfig(), true);
original.recycle();
... I get an outofmemoryerror on the copy line:
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
ERROR/GraphicsJNI(419): VM won't let us allocate 6000000 bytes
Why does the copy instruction need 6MB (!) to generate 1000 × 1500 pixel bitmap?
How can I create a mutable bitmap from a non mutable bitmap in a more memory efficient manner?
edit
Bitmapfactory returns an immutable bitmap. Obviously, the only way to create a variable bitmap from an immutable bitmap is to copy it to a new variable bitmap. In 1000 × In the case of 1500 bitmaps, this obviously requires 12MB (1000x1500x4x2), which leads to outofmemoryerror in most Android devices
Can't this problem be solved in Android?
Answer your first question:
1000 * 1500 * 32 / 8 = 6 million
(color information of 32 bits / pixel)
To answer the second question: you need to reduce the size of the image by processing the image in blocks, or reduce the resolution or color depth