Coas Mckey
Coas Mckey

Reputation: 709

How to get scaled bitmap with respect to screen size with out getting Out of memory exception

In my activity I am creating a Bitmap of the width and height same as the device resolution (width and height )

what I am doing

Bitmap mBitmap = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.ARGB_8888);

and screenWidth and screenHeight is

    screenHeight = displaymetrics.heightPixels;
    screenWidth = displaymetrics.widthPixels;

Now if I make this bitmap I get my heap goes up to 19mb , which is not so good.

So tell me 2 things

1. What is a good way of creating the bitmap with respect to screen with and height with minimum memory consumed

2. How can I destroy the bitmap after using it ?

Please provide me a little source code or link of source code.

Upvotes: 3

Views: 1986

Answers (1)

Henry
Henry

Reputation: 17841

  1. What is a good way of creating the bitmap with respect to screen with and height with minimum memory consumed ?

    1. Never ever create a Bitmap with the screen height or width, because that will be very huge based on the density of the device.Instead use the height and width of the screen and calculate the aspect ratio. Now fix the height to a constant value (like 1200px) and then calculate the width based on the aspect ratio. The ImageView or any other view will scale this properly.
    2. If you really want transparency then use Bitmap.Config.ARGB_8888, if not could use RGB_565 or something else from the list here: http://developer.android.com/reference/android/graphics/Bitmap.Config.html
    3. Follow below points if you are not creating a fresh bitmap but decoding the bitmap from some resource:
      1. You need to use BitmapFactory.Options.inSampleSize to reduce the sampling. So how to calculate the inSampleSize if you already know the height and width? Refer this here: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
      2. So you do all this, and still the inSampleSize is not high enough and you get the OutOfMemoryError ? Don't worry, if this is the case, you need to catch this OutOfMemoryError and in that increase your inSampleSize and do the bitmap decode again. Put this logic in a loop, so the crash never happens.

  2. How can I destroy the bitmap after using it ?

    1. This is really simple. Make sure to call the Bitmap.recycle() method. And remove all reference to the bitmap. Once you do this, the resource will be released and GC will do the cleaning up.

UPDATE: When you create the a bitmap with screen_width and screen_height you would end up with a huge bitmap. Instead create a less resolution bitmap that fits the whole screen. You can see the following code on how this is done.

    float screenHeight = displaymetrics.heightPixels;
    float screenWidth = displaymetrics.widthPixels;

    float aspectRatio  = screenWidth/screenHeight;

    int modifiedScreenHeight = 1000;
    int modifiedScreenWidth = (int) (modifiedScreenHeight * aspectRatio);

    Bitmap mBitmap = Bitmap.createBitmap(modifiedScreenWidth, modifiedScreenHeight, Bitmap.Config.ARGB_8888);

Now you got the bitmap with the right aspect ratio. You could use this bitmap as it is in the ImageView to fill the whole screen. Make sure you put android:scaleType="fitXY" for the ImageView.

Upvotes: 3

Related Questions