Reputation: 7248
I am trying to extend a scalable image viewer in Android to use two images side-by side. To do this I am using Bitmap.createBitmap(int, int, Bitmap.Config)
. Unfortunately this seems to be crashing the VM.
The code is
protected void combineBitmaps() {
image0Width = bitmap0.getWidth();
image0Height = bitmap0.getHeight();
image1Width = bitmap1.getWidth();
image1Height = bitmap1.getHeight();
//These methods just get correct values for image[0|1][Width|Height].
int width = getCanvasWidth();
int height = getCanvasHeight();
//THIS LINE CRASHES
Bitmap bitmap_tmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap = bitmap_tmp;
Canvas combination = new Canvas(bitmap);
combination.drawBitmap(bitmap0, 0f, getImage0VertOffset(), null);
combination.drawBitmap(bitmap1, image0Width, getImage1VertOffset(), null);
}
The value I am getting from getCanvasWidth()
is 2464 and from getCanvasHeight()
is 2048. The exception I am getting is
Unable to start activity ComponentInfo{com.tse.scview/com.tse.scview.ScalableViewWrapper}: android.view.InflateException: Binary XML file line #8: Error inflating class com.tse.scview.ScalableFacingPageView
where com.tse.scview.ScalableFacingPageView
is the class we're constructing (the constructor calls this function).
I've isolated everything from this line—the assignment of the bitmap to a class bitmap, the get width and height functions etc.—to determine that it IS in fact Bitmap.createBitmap that is causing the problem. Unfortunately I can't work out what the problem is.
Update: as a sanity check I put in small values for the width and height (12 and 13 ... well why not). It now works. So it's a RAM problem, which is a pain for us, but effectively answers the question.
Upvotes: 2
Views: 7727
Reputation: 206
I've read that you can allocate more memory for Bitmaps if you use an OpenGL texture, since that doesn't count towards Android's maximum memory per app restriction. Another way is allocating memory in the ndk which also doesn't count towards Android's maximum memory per app restriction.
Upvotes: 0
Reputation: 718718
If I am reading your code and question correctly, the bitmap is 2464 x 2048 x 4 bytes. That's 16 megabytes. And you are creating two of them. That's 32 megabytes.
Upvotes: 1