简体   繁体   中英

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

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.

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.

Uri uri = null;
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
  uri = data.getData();
}
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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM