0

I am trying to load a very large image in my app (2000x3000 and 600kb). I manage to make it open with this code:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

But I get an OutOfMemoryError when I open it 3 times or try to open another activity. How I can fix it?

thanks

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
user3231711
  • 89
  • 3
  • 9
  • 1
    2000x3000 is nearly 24MBs! (File size is not relevant since the file is compressed). What resolution did request of the scaling? Memory needed is height x width x 4 bytes. If you get close to 16MB, you're in trouble but please search the MANY questions and answers on here for how to deal with this. – Simon Jan 30 '14 at 10:49
  • 1
    possible duplicate of [Strange out of memory issue while loading an image to a Bitmap object](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – Simon Jan 30 '14 at 10:50
  • I searched and I found my current code. I want to know if the problem is only the code or the image is too large – user3231711 Jan 30 '14 at 10:54
  • Without knowing what resolution you asked to scale to, that is impossible to answer. If I had an image which is 3000x2000 and scaled it to 10x10, then no, the image is not to large. If I scaled it to 2999x1999 then yes, the image is too large. The code above works as advertised. – Simon Jan 30 '14 at 11:00
  • the resolution is DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);metrics.widthPixels and metrics.heightPixels; In my phone(320x480) – user3231711 Jan 30 '14 at 11:06
  • I think you should do some more reading. That doesn't answer the question since you have not told us the resolution of the device you are using. – Simon Jan 30 '14 at 11:08
  • I think if I told you: 320x480 – user3231711 Jan 30 '14 at 11:15

1 Answers1

0

"But I get an OutOfMemoryError when I open it 3 times or try to open another activity. How I can fix it?"

Release your resources! Call bitmap.recycle() and null the bitmap.

Magnus
  • 1,483
  • 11
  • 14