I have an Android app which loads about 30 Bitmaps into memory. These are jpg resources which are 455x320 px.
This works on all devices I've tested from a G1 to a Galaxy Nexus.
I have another version of this app which is a LiveWallpaper. It works on a Nexus One, Milestone, Galaxy S2, and some 3.x tablets. However, the LWP version of my app crashes with OutOfMemory errors only on the Galaxy Nexus (on ICS).
The following is a simplified version of the code I'm using:
Bitmap bitmap = BitmapFactory.decodeResource(
lwpService.getResources(), R.drawable.somepic);
imageCache.put(R.drawable.somepic, bitmap);
bitmap = BitmapFactory.decodeResource(
lwpService.getResources(), R.drawable.someotherpic);
imageCache.put(R.drawable.someotherpic, bitmap);
… // and so on for 30 more images.
Here is the stack trace from logcat:
02-12 00:07:34.456 E/dalvikvm-heap( 6938): Out of memory on a 583696-byte allocation.
02-12 00:07:34.456 I/dalvikvm( 6938): "Thread-7378" prio=5 tid=16 RUNNABLE
02-12 00:07:34.456 I/dalvikvm( 6938): | group="main" sCount=0 dsCount=0 obj=0x4186c3f8 self=0x20e538
02-12 00:07:34.456 I/dalvikvm( 6938): | sysTid=7115 nice=0 sched=0/0 cgrp=default handle=2213784
02-12 00:07:34.456 I/dalvikvm( 6938): | schedstat=( 0 0 0 ) utm=6907 stm=504 core=1
02-12 00:07:34.456 I/dalvikvm( 6938): at android.graphics.Bitmap.nativeCreate(Native Method)
02-12 00:07:34.456 I/dalvikvm( 6938): at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
02-12 00:07:34.456 I/dalvikvm( 6938): at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
02-12 00:07:34.456 I/dalvikvm( 6938): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
02-12 00:07:34.456 I/dalvikvm( 6938): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
02-12 00:07:34.456 I/dalvikvm( 6938): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
02-12 00:07:34.456 I/dalvikvm( 6938): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
02-12 00:07:34.456 I/dalvikvm( 6938): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:374)
02-12 00:07:34.463 I/dalvikvm( 6938): at com.myapp.loadImage(MyApp.java:155)
Has anybody else had issues loading multiple Bitmaps into memory in a LWP on a Galaxy Nexus?
EDIT: I have found a way to avoid the OutOfMemoryErrors: since my specific images are opaque, I don't need an alpha channel, so I can use Bitmap.Config.RGB_565 instead of Bitmap.Config.RGB_8888. This way, my images are using half the memory as previously.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeResource(
lwpService.getResources(), R.drawable.somepic, options);
I don't think this solution would scale for more or larger images, so I'd still be interested in other comments.