So yesterday I finished with this:

As said, it might turn out to be a too big memory hog, but for now this is fine.

Right. well.

A simple bit of math: A shot with resolution 2145×3217 (o.k., I know that’s not a standard phone-shot, but that shouldn’t be lethal). That’s 6900465 pixels, resulting in an allocation of 13800930 bytes. With 16MB heap that’s a bit steep.  This results in ye olde memory error:

ERROR/dalvikvm-heap(1064): 13800930-byte external allocation too large for this process.
ERROR/(1064): VM won’t let us allocate 13800930 bytes
WARN/dalvikvm(1064): threadid=17: thread exiting with uncaught exception (group=0x4000fe70)
ERROR/AndroidRuntime(1064): Uncaught handler: thread Thread-9 exiting due to uncaught exception
ERROR/AndroidRuntime(1064): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

I’ve tried to skip the whole bitmap and just play with a drawable, but that doesn’t help either. BitmapFactory still needs to get into the mix and allocate your pixels.

In the end the solution was of course really simple, and everyone will be thinking “duh”, but still; for the thumbnail all those pixels are a waste, so we can resample without any trouble:

BitmapFactory.Options options = new BitmapFactory.Options();
thumb = BitmapFactory.decodeStream(in,null,options);

I’ve used the stream here because that was the quickest, but there is a decode for everybody:
decodeFile, decodeResource…Just pick your flavour :)

This works perfectly to show “a small version of the picture” aka thumbnail, so I’m happy with that :). Only possible trouble might be that this doesn’t work on higher API levels, but I’ll test that in a sec and don’t expect any trouble.

Comments are closed.