miha miha - 5 months ago 24
Android Question

Handling large bitmaps on Android - int[] larger than max heap size

I'm using very large bitmaps and I store data in a big int[]. The images can be really large and I can't downsample them (I'm getting the bitmaps over the wire and rendering them).

The problem I'm hitting is on very large bitmaps (bitmap size = 64MB), where I try to allocate int array with size 16384000. I'm testing this on Samsung Galaxy SII, which should have enough memory to handle this, but it seems there is a "cap" on heap size. The method

Runtime.getRuntime().maxMemory()
returns 64MB, so this is the max heap size for this particular device.

The API level is set to 10, so I can't use
android:largeHeap
attribute suggested elsewhere (and I don't even know if that would help).

Is there any way to allocate more than 64MB? I tried allocating the array in native (using JNI
NewIntArray
function), but that fails as well. It seems as though it is bound by the same limit as jvm.

I could however allocate memory on the native side using
NewDirectByteBuffer
, but since this byte buffer is not backed by an array, I can not get access to
int[]
(using
asIntBuffer().array()
in java which I need in order to display the image using
setPixels
or
createBitmap
. I guess OpenGL would be a way to go, but I have (so far) 0 experience with OpenGL.

Is there a way to somehow access allocated memory as int[] that I am missing?

Answer

So, the only way I've found so far is to allocate image using NDK. Furthermore, since Bitmap does not use existing Buffer as pixel "storage" (the method copyPixelsFromBuffer is also bound to memory limits; and judging by the method name, it copies the data anyway).

The solution (I've only prototyped it roughly) is to malloc whatever the size of the image is, and fill it using c/c++ and then use ByteBuffer in Java with OpenGLES.

The current prototype creates a simple plane and applies this image as a texture to it (luckily, OpenGLES methods take Buffer as input, which seems to work as expected). I'm using glTexImage2D to apply this buffer as a texture to a plane. Here is a sample, where mImageData is ByteBuffer allocated (and filled) on the native side.

int[] textures = new int[1];
gl.glGenTextures(1, textures, 0);
mTextureId = textures[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, 4000, 4096, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, mImageData);