24

I have this code:

startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), CAMERA_IMAGE);

That allows that user to take a photo. Now how would I get the Uri of that photo in onActivityResult? Is it an Intent extra? Is it through Intent.getData()?

Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251

4 Answers4

34
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    Uri u = intent.getData();
}

By the way... there's a bug with that intent in some devices. Take a look at this answer to know how to workaround it.

Community
  • 1
  • 1
Cristian
  • 198,401
  • 62
  • 356
  • 264
9

Instead of just launching the intent, also make sure to tell the intent where you want the photo.

Uri uri = Uri.parse("file://somewhere_that_you_choose");
Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photoIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(photoIntent, CAMERA_IMAGE);

Then when you get your onActivityResult() method called, if it was a success just open a stream to the URI and it should all be set.

Greg Giacovelli
  • 10,164
  • 2
  • 47
  • 64
  • 4
    The problem with this approach is that on many devices the image is stored in the location specified by the EXTRA_OUTPUT extra AND in the gallery. I have personally seen this on a few HTC and LG devices but I know there are others out there that have this same problem... – Justin Jan 20 '12 at 23:47
  • 1
    I think you are seeing a misunderstanding of how the gallery works. If you don't delete the photo at some point depending on where you ask it to save, it will appear in your gallery as just another photo on your phone. If you set the bundle value of EXTRA_OUTPUT to be a sandboxed folder that you have access to and has an empty file named ".nomedia" in it, the gallery does not scan the output. – Greg Giacovelli Jan 22 '12 at 06:16
  • That is not the behavior I am seeing... there is a known bug on many devices causing the duplicate save that I mentioned. – Justin Jan 31 '12 at 21:09
  • Take a look here... It explains the problem, and also has a link to the bug in the bug tracker: http://stackoverflow.com/questions/1910608/android-action-image-capture-intent – Justin Feb 06 '12 at 20:05
  • Also, on my phone (LG G2x) I experimented with taking about 15 photos and saved them all to the same location with the same file name. Afterwards, I looked in my gallery and all 15 images were still in the gallery. – Justin Feb 06 '12 at 20:07
  • The bug you link to is regarding the image size of the capture, not a double capture issue. Either way, both with and without EXTRA_OUTPUT work. The getData() without EXTRA_OUTPUT is just a lower res photo (usually thumb sized), EXTRA_OUTPUT has never been full res but is usually higher. There were more problems with earlier phones/OS builds. But I haven't seen too much of those issues in the most recent phones/builds. – Greg Giacovelli Feb 11 '12 at 07:10
  • 1
    Yeah... looking at my link that was the wrong post. I'm having a hard time finding the post again. But I still have tested this and get the results I mentioned on my LG phone and on my HTC phone. – Justin Feb 14 '12 at 21:51
2
Uri uri = null;
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
  uri = data.getData();
}
Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
anson
  • 1,436
  • 14
  • 16
0
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {

          Bitmap photo = (Bitmap) data.getExtras().get("data");
          Uri fileUri = Utils.getUri(getActivity(), photo);
    }  
}

public String getRealPathFromURI (Uri contentUri) {
    String path = null;
    String[] proj = { MediaStore.MediaColumns.DATA };
    Cursor cursor = getActivity().getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor.moveToFirst()) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        path = cursor.getString(column_index);
    }
    cursor.close();
    return path;
}
Vignes
  • 390
  • 4
  • 11