0

I want to load a big size (about 10 MB) image and let user add some effects (such as Grayscale and so on). So, I loaded the bitmap using inSampleSize to show a preview to user:

public Bitmap load(Context context, int[] holderDimention, String image_url) throws Exception {
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(image_url, bitmapOptions);

    int inSampleSize = 1;

    final int outWidth = bitmapOptions.outWidth;
    final int outHeight = bitmapOptions.outHeight;

    final int holderWidth = holderDimention[0];
    final int holderHeight = holderDimention[1];

    // Calculation inSampleSize
    if (outHeight > holderHeight || outWidth > holderWidth) {
        final int halfWidth = outWidth / 2;
        final int halfHeight = outHeight / 2;

        while ((halfHeight / inSampleSize) > holderHeight && (halfWidth / inSampleSize) > holderWidth) {
            inSampleSize *= 2;
        }
    }

    bitmapOptions.inSampleSize = inSampleSize;

    // Decoding bitmap
    bitmapOptions.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(image_url, bitmapOptions);
}

Finally,in background I want to apply effects on the original image without downsampling (it is necessary). So I used the following code inside a subclass of AsyncTask:

public Bitmap load(Context context, String image_url) throws IOException {
    return BitmapFactory.decodeFile(image_url);
}

But I got an out of memory exception. How could I handle this problem?

  • You should add a `try{} catch (OutOfMemoryException e){}` in your code to avoid crash. This is a big issue with ALL android devices. Anrdoid applications have a maximum of 24MB RAM available so bitmap operations are hard to manage. – Manitoba Sep 17 '14 at 13:43

1 Answers1

2

You need to do it partially. Load only part of the image, process, save part. Check out BitmapRegionDecoder.

Related question: Crop image without loading into memory

Community
  • 1
  • 1
Ivan
  • 1,735
  • 1
  • 17
  • 26