java.lang.OutOfMemoryError in android while getting image from gallery in android

后端 未结 4 1128
走了就别回头了
走了就别回头了 2020-12-30 11:32

I am picking a picture from gallery using code

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCont         


        
4条回答
  •  长发绾君心
    2020-12-30 12:09

    The image you are trying to load is too large. That it causing the heap size to grow rapidly and filling it up, which is causing the app to crash. Try to downsize the image and then load it.

    You can use the following code snippet:

    public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
        int width = image.getWidth();
        int height = image.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(image, 0, 0, width, height,
                matrix, false);
        return resizedBitmap;
    }
    

提交回复
热议问题