2

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);
Bogdan Daniel
  • 2,689
  • 11
  • 43
  • 76
  • 1
    Calling getLayoutParams on the imageView may be the issue. Since this code is located within your onActivityResult method onResume may not have been called and if galleryImage has not yet had is params set (depending entirely on how/where you are setting those params) getLayoutParams may not be returning what you want or anticipated. (Up votes would be great :)) – Devsil Nov 12 '15 at 18:20
  • @Devsil so how can I solve this ? – Bogdan Daniel Nov 12 '15 at 18:48
  • Well, you should have an idea of how big you want the image to be, so you can set it with ResizeOptions(50,50) or whatever your desired width and height are. If that is not that case, you can pull fresco code from the onActivityResult method and do it all in onResume after you are sure that the layoutparams for the parent view have been set and will return a value that is acceptable. – Devsil Nov 12 '15 at 19:18
  • Thank you. I changed the parameters from match_parent to 50dp and everything is working fine. Could you also take a look here http://stackoverflow.com/questions/33661308/fresco-image-with-both-width-and-height-match-parent-doesnt-display ? Maybe you have some ideas. – Bogdan Daniel Nov 12 '15 at 19:26

2 Answers2

4
ImagePipelineConfig frescoConfig = ImagePipelineConfig.newBuilder(getApplicationContext()) .setDownsampleEnabled(true).build();
Fresco.initialize(this, frescoConfig);

It's working for me.

Alan
  • 66
  • 4
1

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);
Devsil
  • 598
  • 3
  • 16
  • Where should I put that ? – Bogdan Daniel Nov 12 '15 at 17:21
  • Well you will need to set the controller of the SimpleDraweeView. That example I gave you is being using in a class that extends DraweeView. So before you use setImage with galleryImage.setImageURI you will first build the ImageRequestBuilder then the DraweeController and finally after both those are built you will call something like galleryImage.setControlle(controller) – Devsil Nov 12 '15 at 17:30
  • I modified everything accordingly. I don't get an errors in the logcat but the try block fails and I get back as a toast "Something went wrong" – Bogdan Daniel Nov 12 '15 at 17:57
  • How are you setting the Resize Options? With a specific height and width or are you using the getLayoutParams of the parent view? If you are using the getLayoutParams method i would suggest setting the size using specific integers and see if that resolves the issue. Also, just to be clear if you are setting a controller you HAVE to call galleryImage.setOldController(galleryImage.getController()) AND be sure you are not also still calling the setImageURI method. The ImageRequestBuilder and controller will take care of that. Also in that catch method print the stacktrace and post here if need b. – Devsil Nov 12 '15 at 18:01
  • I will edit with my method. I called getLayoutParams on the image. – Bogdan Daniel Nov 12 '15 at 18:06