user1312837
user1312837

Reputation: 1298

java.lang.outofmemoryerror on create bitmap

I receive big image (png, >30Mb) and create bitmap for it. And get java.lang.OutOfMemoryError. I try to catch such exception:

try {
    Bitmap bmp = BitmapFactory.decodeStream(someStream);
} catch (OutOfMemoryError e) {
    ;
}

With 2.2 SDK it works well. But with 2.3 app fails with uncatched exception. I don't understand, why?

Thanks!

Upvotes: 0

Views: 3562

Answers (4)

N-JOY
N-JOY

Reputation: 7635

you need to re-size bitmap images before you could use them to display. Have a look at this tutorial about how to re-size bitmaps.

EDIT alternatively you could try to save bitmap as file to filesystem using FileOutputStream. Something like this.

byte[] imageData;
FileOutputStream out = new FileOutputStream(file);
out.write(imageData, 0, imageData.length);
out.flush();
out.close();

decode the bitmap something like this, and scale/resize it as specified in above link.

FileInputStream inputStream = new FileInputStream(file);
BitmapFactory.decodeStream(inputStream, null, options);

Upvotes: 1

Greg Kopff
Greg Kopff

Reputation: 16575

You're not really meant to catch Errors:

From the Javadoc:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

If you get an Error, especially an OutOfMemoryError, it's basically already too late to do anything about it.

Upvotes: 6

Dhruv Gairola
Dhruv Gairola

Reputation: 9182

Decode the image to a lower resolution. You're only allowed about 16mb on android. In the worst case, you can choose to increase this setting.

Also, pre-gingerbread GC is different from post-gingerbread. Before it was a stop the world GC with full garbage collection, but after that, the GC became concurrent, allowing partial collection.

Upvotes: 0

Akhil
Akhil

Reputation: 14038

30 mb is just too large. I am very surprised that you were able to create a bitmap out of it in an android 2.2 phone. My advice is to use a much smaller image file. In many phones 24-30mb is the memory given to your whole app.

In your case you must be getting an Error such as StackOverFlowError. You cant catch errors,

Upvotes: 0

Related Questions