ipman
ipman

Reputation: 1222

How to resize a picture according to the screen width of the phone on Android?

I want user to select a picture from gallery. Since size of most pictures are very large I want to scale it to the most appropriate width. Is there a way to do that when I only know the path of the picture?

Upvotes: 1

Views: 909

Answers (1)

Dmitry Zaytsev
Dmitry Zaytsev

Reputation: 23962

For example, if your Bitmap is bmp, and ImageView is framePhoto:

        int iWidth=bmp.getWidth();
        int iHeight=bmp.getHeight();

        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);

        int dWidth=dm.widthPixels;
        int dHeight=dm.heightPixels;

        float sWidth=((float) dWidth)/iWidth;
        float sHeight=((float) dHeight)/iHeight;

        if(sWidth>sHeight) sWidth=sHeight;
        else sHeight=sWidth;

        Matrix matrix=new Matrix();
        matrix.postScale(sWidth,sHeight);
        newImage=Bitmap.createBitmap(bmp, 0, 0, iWidth, iHeight, matrix, true);
        framePhoto.setImageBitmap(newImage);

Upvotes: 2

Related Questions