Search code examples
androidimage-processingout-of-memoryandroid-image

Android BitmapFactory not loading dimensions


I know there are lots of threads on the topic "loading images with android", but unfortunately I didn't find a solution to my problem in any of them. So here's my problem:

I want to save a big image and another one to be used for cropping later, here's my code:

BitmapFactory.Options bmOptions = new BitmapFactory.Options();

bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image, bmOptions);

final int REQUIRED_SIZE = 800;
int scale = 1;
while (bmOptions.outWidth / scale / 2 >= REQUIRED_SIZE && bmOptions.outHeight / scale / 2 >= REQUIRED_SIZE) {
    scale *= 2;
}

bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scale;
bmOptions.inPurgeable = true;
bmOptions.inInputShareable = true;

// create the Image for the original File
try {
    File origFile = File.createTempFile(origImage, JPEG_FILE_SUFFIX, getAlbumDir());
    OutputStream fOut2 = new FileOutputStream(origFile);
    Bitmap thePic = BitmapFactory.decodeFile(image, bmOptions);
    thePic.compress(Bitmap.CompressFormat.JPEG, 100, fOut2);
    fOut2.flush();
    fOut2.close();
} catch (Exception e) {
    Log.e(TAG, "Cannot create original Image");
}

mImageBitmap = BitmapFactory.decodeFile(image, bmOptions);

This code works ~90% of the time. But sometimes bmOptions.outHeight & bmOptions.outWidth returns -1 and in the line Bitmap thePic = BitmapFactory.decodeFile(image, bmOptions);

I get the exception:

10-31 12:07:31.645: E/AndroidRuntime(16618): java.lang.OutOfMemoryError
10-31 12:07:31.645: E/AndroidRuntime(16618):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
10-31 12:07:31.645: E/AndroidRuntime(16618):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:652)
10-31 12:07:31.645: E/AndroidRuntime(16618):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391)

What I think the problem could be:

  • I take multiple fotos back to back and after a while this error may occur
  • it happens when I turn my display while taking the foto

But after lots of testing I'm still not sure if any of this two possibilities is true, or if something else is wrong.

Has anyone any idea what I'm doing wrong?


edit:

after lots of testing with my app I have the following problem:

Every time I start the process I loose ~2 MB of RAM. Which means after some time my app will close.

What I did to solve this problem:

-) dont create the original image -) set the sample size to 16 (instead of the calculated 2) -) removed the bitmap completely

The problem stays the same; I always loose 2 MB RAM. Has anyone any idea what the problem could be?


Solution

  • After these two lines...

    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(image, bmOptions);
    

    ...bmOptions will contain a valid outHeight and outWidth only if the decode worked. If not, they will contain -1 as you have noted.

    You'll need to debug to verify, but I suspect that image is sometimes null or invalid in some manner.

    You say this happens when you take multiple photos back to back. Perhaps the Garbage Collector can't keep up or you aren't releasing the references to the images/bitmaps as you process them? In what manner are you triggering the camera and capturing the result?

    As for your second point about rotation, this could be the issue:

    • Your app/activity is in portrait
    • You trigger the camera, which starts in portrait
    • You rotate to landscape whilst in the camera
    • You return to your app/activity which is immediately recreated for landscape. Could you be losing your reference to image at this point?