1

I have a list in my application that contains all the installed applications with their icons, I'm able to render the installed applications and the icons as well but it is consuming lot of memory as it loads the drawables(icons) of the installed applications into memory as well.

I want to scale down the icon and then load into memory just to reduce the memory usage of the application. Can anyone tell me how that can be achieved.

Note : if PNG format then it will not compress your image because PNG is a lossless format. and applications icons are in PNG format

Any way to reduce the memory allocation for the icons??

Neji
  • 6,591
  • 5
  • 43
  • 66
  • 1
    Check this out first: http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966 – g00dy Jul 31 '13 at 10:43
  • PNG format then it will not compress your image because PNG is a lossless format. And i want to load installed applications icons in my listview – Neji Aug 02 '13 at 09:41
  • It depends, but basically PNG could have a compression. Which part of code did you consider and you think that works only with png? – g00dy Aug 02 '13 at 11:00
  • I want to scale down the icon and then load into memory just to reduce the memory usage of the application. Lookin for solution for the same – Neji Aug 02 '13 at 11:03
  • First of all, there's a difference between scaling down (resizing) and compression (image processing). From the link I provided above, (see the answer with over 400 positive votes total), there's a pretty slick method (`decodeFile`) to reduce the memory usage. – g00dy Aug 02 '13 at 11:10

2 Answers2

1

Yeah it's all in the docs:

http://developer.android.com/training/displaying-bitmaps/index.html

Calculate your sample size, i.e. size of the bitmap you want:

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) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

The decode your bitmap at this size:

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);
}

This should all be done off the UI thread:

http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

You can then cache the bitmaps so you don't have to do it more times than necessary:

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • PNG format then it will not compress your image because PNG is a lossless format. and PNG is the format of applications icons – Neji Aug 02 '13 at 09:37
1

Use inSampleSize from BitmapFactory.Options

    BitmapFactory.Options options = new BitmapFactory.Options();
 options.inSampleSize = 2; //Downsample 10x
Sachin Thampan
  • 893
  • 7
  • 17