How to resize image before loading to ImageView to avoid OOM issues

后端 未结 4 1632
忘了有多久
忘了有多久 2021-01-24 04:18

How can i resize image before loading to imageview after selecting from gallery/photos?. Otherwise large images are causing OOM issues.

SelectImageGallery.setOnC         


        
4条回答
  •  耶瑟儿~
    2021-01-24 04:55

    refer to this answer

    you can resize your bitmap like this

    import android.graphics.Matrix
    
    public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
        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);
        bm.recycle();
        return resizedBitmap;
    }
    

提交回复
热议问题