0

I'm trying to decode a drawable resource into a bitmap like below.

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = true;
        int destinationDimension = 128;
        options.outHeight = destinationDimension;
        options.outWidth = destinationDimension;
        bmp = BitmapFactory.decodeResource(context.getResources(), defaultImage, options);
        Log.d(TAG, "height = " + bmp.getHeight() + " width = " + bmp.getWidth());

But somehow the decoded bitmap is of dimesion 1344x1344.

    height = 1344 width = 1344

How can i get bitmap of dimension 128x128.

Shashank Degloorkar
  • 3,151
  • 4
  • 32
  • 50

1 Answers1

0

options.outHeight is used as an output of BitmapFactory.decodeResource.

You can clearly identify them by options.out<...>. So after we decodeResource the Bitmap, the resulting height and width will be written into options.outHeight and options.outWidth

So, can we resize bitmap in decodeResource?

Yes, and No.

We can resize by using inSampleSize. However, this will scale the bitmap for 2^n. Detail see here for calculation for inSampleSize: https://developer.android.com/topic/performance/graphics/load-bitmap

To scale bitmap to our destination width/height, use Bitmap.createScaledBitmap. See here: How to resize image (Bitmap) to a given size?.

Hendry Setiadi
  • 241
  • 1
  • 3