Search code examples
javaandroidimagememoryscale

Efficient Image Resizing


In short, is the following an inefficient way to rescale images in any way perhaps due to garbage collection?

playBg_ = BitmapFactory.decodeResource(res_, R.drawable.field);
playBg_ = Bitmap.createScaledBitmap(playBg_, screenWidth_, screenHeight_, false);

And would it then be better to do something like this?

Bitmap tempBmp = BitmapFactory.decodeResource(res_, R.drawable.field);
playBg_ = Bitmap.createScaledBitmap(tempBmp, screenWidth_, screenHeight_, false);
tempBmp.recycle();

Or is there a better way?

Thanks!


Solution

  • I found that the second method was required. The first method left too much memory wasted before garbage collection could get to it. I also found that I definitely need to call recycle on all my images prior to loading up new ones. Simply dereferencing them and expecting garbage collection to pick it up is not sufficient. I was constantly running out of memory due to this fact.