0

I am using some static images in my application and the images is being kept in the drawable folder.The size of the images are near about 2 MB but I have scaled them properly but still it is showing out of memory error due to the bitmap size during run time.And this is specially for samsung galaxy s3. Can anybody plz tell me how to stop this and to reduce the bitmap size.

I have tried using this code for recycling images:

 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 = 8;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    }
    return inSampleSize;
}

1 Answers1

0

First compress the images to check if actually these are the ones that leak memory.

Get used to using tools to measure memory you are using and finding leaks because simply this is a way to do it. You might start here: What Android tools and methods work best to find memory/resource leaks?

I also recomend this code for image viewing https://github.com/nostra13/Android-Universal-Image-Loader

Community
  • 1
  • 1
AndroidGecko
  • 14,034
  • 3
  • 35
  • 49