Search code examples
androidimageandroid-resourcesandroid-bitmapbitmapfactory

Why does BitmapFactory scale down my image by a factor of 1.333 instead of 2?


I have a PNG image file with 2236x971px dimensions as a resource. I want to scale it down by a factor of two (to its half). However, when i use this code:

BitmapFactory.Options bo = new BitmapFactory.Options();
bo.inSampleSize = 2;
Bitmap decodedBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image, bo);

the decodedBitmap.getWidth() and decodedBitmap.getHeight() show width: 1677, height:728 → Only 25% reduction in size instead of expected 50%. Why is that so?

I am running the code on API 19.


Solution

  • The reason is that, your resource gets loaded according to your screen metrics. Put your image in the drawable-nodpi Folder or open an input stream to your resource and decode that input stream.