Android how to scale an image with BitmapFactory Options

社会主义新天地 提交于 2019-12-01 02:21:28
jbowes

Use BitmapFactory.Options.inSampleSize when you first load your image to get the size as close as possible to your target size, then use Bitmap.createScaledBitmap to scale to the exact size you want.

There's some code for this in this answer.

Start by setting inJustDecodeBounds to true in your Options, this will give you the size of the image, without loading any of the image data.
If the image size is smaller then your max size, then just load it as usual.
If on the other hand, it is too big, use the inSampleSize to read a smaller bitmap.

Note: I don't think that the decoding does any filtering when using the inSampleSize, so you might lose some detail, but as your image size is so big (2048 px) I don't think this will cause any problems.

Shishir Shetty

You can try something like this:

int imageWidth, imageHeight;

Bitmap result = Bitmap.createScaledBitmap(bitmapPicture,
                        imageWidth, imageHeight, false);

Here you can add your own width and height. bitmapPicture is the object of Bitmap. Let me know if this is of any help to you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!