Search code examples
androidbitmapdrawableandroid-resourcesbitmapfactory

Why does BitmapFactory.decodeResource scale my image?


I have an image that is 120x120px located in my /res/drawable/ directory. This is the size required for all devices.

To load this Bitmap, I am using the following:

Bitmap tmpBmp = BitmapFactory.decodeResource(getResources(), R.drawable.myimage);

The issue is that when I measure tmpBmp, the size is 360x360px. I know how to scale this back down to the 120x120 that I need, but it is a waste of processing time, although minimal.

I am assuming that this has something to do with screen densities, since my resource is in a density-less directory.

What is the reason for decodeResource scaling my image? And how do I stop it from doing so?


Solution

  • The default drawable directory assumes that images need to be scaled from the default mdpi size. Put your images in drawable-nodpi if you want to disable resource scaling.

    Do note that a 120x120px image, if displayed on the screen, will be 3x smaller on a xxhdpi device compared to a mdpi device (as there are three times as many pixels per inch).