How to resize an image i picked from the gallery in android?

后端 未结 2 918
盖世英雄少女心
盖世英雄少女心 2020-12-09 12:00

I am building an android where. Inside of one activity I have an image button. When I click on it the gallery opens up and I can choose an image. Then I set that image as th

2条回答
  •  心在旅途
    2020-12-09 12:13

    Refer this LINK

    Use: Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)

    or use these method::

    public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
    
        // create a matrix for the manipulation
        Matrix matrix = new Matrix();
    
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
    
        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    
        return resizedBitmap;
    }
    

提交回复
热议问题