0

I'm trying to get image from the camera or the gallery (whatever the user chose) in one intent. the problem is that I always get intent.getData() as null in OnActivityResult. I'm doing the following as suggested here:

Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String pickTitle = "Select or take a new Picture"; // Or get from strings.xml
Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { takePhotoIntent });

startActivityForResult(chooserIntent, SELECT_PICTURE);

OnActivityResult:

if (requestCode == SELECT_PICTURE && resultCode == Activity.RESULT_OK) {
        if (data != null) {
            try {
                final Uri imageUri = data.getData();
                Log.e("uri", imageUri + "");

uri is null

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
ben
  • 1,064
  • 3
  • 15
  • 29
  • `ACTION_IMAGE_CAPTURE` does not return a `Uri`. In your case, `data.getParcelableExtra("data")` will return a `Bitmap`. – CommonsWare Nov 02 '17 at 19:06

1 Answers1

0

Well, ACTION_IMAGE_CAPTURE does not return Uri. But if by any chance you really want to get the Uri after taking a photo by using it, then you need to specify the path for the picture (it will save the photo on that path, so it's not temporary).

This is one of that example (this will create directory & prepare the new file for your image):

// Determine Uri of camera image to save.
    File root = new File(
            Environment.getExternalStorageDirectory() + File.separator + locationName + File.separator);
    root.mkdirs();
    String fname                = imagePrefixName + System.currentTimeMillis() + ".jpg";
    File   imageMainDirectory = new File(root, fname);
    Uri    outputFileUri        = Uri.fromFile(imageMainDirectory); //make sure you store this in the place that can be accessed from your onActivityResult

After you decided your outputUri & store it, then you need to implement that uri on your camera intent like this:

takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

Then, when you received the data you can do this to check if the picture is come from gallery or camera (picture that stored has Uri on it):

final Uri imageUri = data.getData();
if (imageUri == null)
{
   imageUri = outputFileUri;
}

Make sure you implement the permission for write & read external storage & with this, i think you will get Uri that you want.

Suhafer
  • 99
  • 4