Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get image Uri in onActivityResult after taking photo?

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()?

like image 322
Mohit Deshpande Avatar asked Feb 20 '11 20:02

Mohit Deshpande


People also ask

How does onActivityResult work on Android?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.

What is onActivityResult?

onActivityResult is the callback you have on the first activity to grab the contacts you choose. Follow this answer to receive notifications.


3 Answers

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.

like image 92
Cristian Avatar answered Oct 05 '22 19:10

Cristian


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.

like image 28
Greg Giacovelli Avatar answered Oct 05 '22 19:10

Greg Giacovelli


Uri uri = null;
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
  uri = data.getData();
}
like image 36
anson Avatar answered Oct 05 '22 18:10

anson