1

How to get URI from Bundle? I have tried the below code, but data.getExtras(); will give error and data.getData() will give null .

Intent intent = new Intent(Intent.ACTION_PICK,

                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            intent.setType("image/*");

            intent.putExtra("scale", true);
            intent.putExtra("crop", "true");
            intent.putExtra("outputX", 256);
            intent.putExtra("outputY", 256);
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, 1);

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {

 final Uri imageUri = data.getExtras();
final Uri imageUri = data.getData();
}
}
djac
  • 187
  • 19
  • Check out [this](https://stackoverflow.com/questions/4233873/how-do-i-get-extra-data-from-intent-on-android) answer. – henrique romao Aug 08 '17 at 11:43
  • data.getExtras() gives a Bundle, docs say the uri is in getData(), but you have to set the intent.setAction(Intent.ACTION_GET_CONTENT); – Matias Olocco Aug 08 '17 at 11:45

2 Answers2

5

How to get URI from Bundle?

You don't, usually.

but data.getExtras(); will give error

That is because getExtras() returns a Bundle, not a Uri.

and data.getData() will give null

Either the user did not pick anything or there is a bug in the ACTION_PICK activity that the user chose for this request.

Also, please note that those extras that you are putting on ACTION_PICK are undocumented, unsupported, and will not be honored on many devices. There are plenty of image cropping libraries available for Android. Please use one.

In addition, ACTION_PICK does not use a MIME type. ACTION_PICK says "pick a piece of content from this collection of content". If you want to limit things by MIME type, use ACTION_GET_CONTENT (and no Uri in the Intent) or ACTION_OPEN_DOCUMENT to say "give me a piece of content that matches this MIME type".

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Add that he has to set the action and is the perfect answer. – Matias Olocco Aug 08 '17 at 11:46
  • my intention is just to get the uri upon selecting an image from Gallery. I have the code for API > 22. I need the code for lollipop. Can you give some example or reference? – djac Aug 08 '17 at 11:46
  • @MatiasOlocco: I am not sure what you are referring to. The question has `new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);`. `ACTION_PICK` is an action. However, I do see that the question has `setType()`, which is not used with `ACTION_PICK`, so I'll add that to my answer. – CommonsWare Aug 08 '17 at 11:52
  • @djac: "my intention is just to get the uri upon selecting an image from Gallery" -- that is not possible in general. `ACTION_PICK` and `ACTION_GET_CONTENT` allow the *user* to choose the activity that responds to that `Intent`, if there is more than one on the device. There is no requirement that the user choose some sort of gallery app. `ACTION_OPEN_DOCUMENT` brings up a system-supplied UI for choosing the content. – CommonsWare Aug 08 '17 at 11:56
  • @CommonsWare perfect! – Matias Olocco Aug 08 '17 at 12:05
0

You forgot intent.setAction(Intent.ACTION_GET_CONTENT);

If you don't add that your data.getData() would be null

Kapil G
  • 4,081
  • 2
  • 20
  • 32