android Image view out of memory error

后端 未结 5 1690
一整个雨季
一整个雨季 2021-01-25 06:43

In my Android project I have imageButton , and after clicking on it , it must open new Activity with imageView , and in my new Activity I must see the ImageButton\'s image only

5条回答
  •  一个人的身影
    2021-01-25 07:15

    Above solutions not works in my case, so I found this it work for me Try this one.

    private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
        BitmapFactory.Options op = new BitmapFactory.Options();
        op.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(
                getActivity().getContentResolver().openInputStream(selectedImage), null, op);
    
        final int REQUIRED_SIZE = 1000;
    
        int width_tmp = op.outWidth, height_tmp = op.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }
    
        BitmapFactory.Options op2 = new BitmapFactory.Options();
        op2.inSampleSize = scale;
        return BitmapFactory.decodeStream(
                getActivity().getContentResolver().openInputStream(selectedImage), null, op2);
    }
    

    Then set returned Bitmap in to your imageView like below

    yourImageView.setImageBitmap(returnedBitmap);
    

提交回复
热议问题