Java how to resize image in android programmatically

Java how to resize image in android programmatically

To resize an image in an Android app programmatically, you can use the Bitmap class and its createScaledBitmap method.

Here is an example of how you can resize an image in Android programmatically:

re‮l:ot ref‬autturi.com
// Load the image from a file
Bitmap originalImage = BitmapFactory.decodeFile("image.png");

// Calculate the desired width and height of the image
int width = originalImage.getWidth() / 2;
int height = originalImage.getHeight() / 2;

// Create a scaled version of the image with the desired width and height
Bitmap scaledImage = Bitmap.createScaledBitmap(originalImage, width, height, true);

// Update the src of an ImageView with the scaled image
imageView.setImageBitmap(scaledImage);

In this example, the BitmapFactory class is used to load the image from a file, and then the createScaledBitmap method of the Bitmap class is used to create a scaled version of the image with the desired width and height. The scaled image is then set as the src of an ImageView using the setImageBitmap method.

Note that this method may result in a loss of quality if the image is scaled up, as the original pixels will be stretched. To avoid this, you can use an image resizing library such as Picasso or Glide, which use more advanced algorithms to resize images without sacrificing quality.

Created Time:2017-11-03 23:27:11  Author:lautturi