Search code examples
androidandroid-studiofresco

Fresco - Bitmap too large to be uploaded into a texture


I'm trying to display an Image from the gallery into a SimpleDraweeView. I don't know if this is the way to do it, but what I'm doing is going into the gallery, selecting the image and getting the image path. After that I use galleryImage.setImageURI(Uri.parse(url)); to set the image into that SimpleDraweeView (that's the type of galleryImage). Unfortunately I get Bitmap too large to be uploaded into a texture (4128x2322, max=4096x4096).

Here is my code that opens the gallery and returns the imagePath.

public void loadImagefromGallery(View view) {
        // Create intent to Open Image applications like Gallery, Google Photos
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        // Start the Intent
        startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
    }

    // When Image is selected from Gallery
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            // When an Image is picked
            if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data

                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                // Get the cursor
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgPath = cursor.getString(columnIndex);
                cursor.close();
                Toast.makeText(this, imgPath,
                        Toast.LENGTH_LONG).show();
                String url = "file://" + imgPath;

                galleryImage.setImageURI(Uri.parse(url));
            } else {
                Toast.makeText(this, "You haven't picked Image",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
        }

    }

Edit:

ImageRequestBuilder imageRequestBuilder = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                    .setAutoRotateEnabled(true)
                    .setResizeOptions(new ResizeOptions(galleryImage.getLayoutParams().width, galleryImage.getLayoutParams().height));

            DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setImageRequest(imageRequestBuilder.build())
                    .setOldController(galleryImage.getController())
                    .build();

            galleryImage.setController(controller);

Solution

  • You will need to use an ImageRequestBuilder and a Drawee Controller to set the resize options so that the images that are too large (over 2048 in either width or height) get resized before being put into a texture. Like this:

    ImageRequestBuilder imageRequestBuilder = ImageRequestBuilder.newBuilderWithSource(Uri.parse(uri))
                .setAutoRotateEnabled(true)
                .setResizeOptions(new ResizeOptions(getLayoutParams().width, getLayoutParams().height));
    
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(imageRequestBuilder.build())
                .setOldController(getController())
                .build();
    
        setController(controller);