BekaKK
BekaKK

Reputation: 2253

android resize bitmap auto height

I try to resize bitmap from gallery.I wrote some code and i can resize bitmap only static width and height.this is source

     public Bitmap getResizedBitmap(Bitmap bm, int newWidth) {

    float aspectRatio = (float)bm.getWidth() / (float) bm.getHeight();
    int height = Math.round(newWidth / aspectRatio); //based on width it gives height
    Matrix matrix = new Matrix();

    matrix.postScale(newWidth, height);

    Bitmap resizedBitmap = Bitmap.createBitmap(
            bm, 0, 0, newWidth, height, matrix, false);
    return resizedBitmap;
}

but i want to resize bitmap for example width 800px and auto height.is it a possible to solve this problem in android ? thanks

Upvotes: 0

Views: 343

Answers (2)

Deepshikha Puri
Deepshikha Puri

Reputation: 2094

public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
Bitmap resizedBitmap;


try {
         resizedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight());
            }catch (Exception e){
                e.printStackTrace();
                boolean_image = true;
                return "";

            }

return resizedBitmap;
}

Upvotes: 1

Sasi Kumar
Sasi Kumar

Reputation: 13368

float aspectRatio = (float)Image.getWidth() / (float) Image.getHeight();
int width = 200;    //your width
int height = Math.round(width / aspectRatio); //based on width it gives height

Upvotes: 4

Related Questions