拍照后获取onActivityResult中的图像Uri?

时间:2011-02-20 20:16:06

标签: java android image camera uri

我有这段代码:

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

允许该用户拍照。现在,如何在Uri中获取该照片的onActivityResult?它是Intent额外的吗?是通过Intent.getData()吗?

4 个答案:

答案 0 :(得分:29)

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

顺便说一句......在某些设备中存在这种意图的错误。请查看this answer以了解如何解决此问题。

答案 1 :(得分:8)

不仅要启动意图,还要确保将意图告诉您想要照片的位置。

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);

然后当你调用onActivityResult()方法时,如果成功,只需打开一个URI的流,它应该全部设置。

答案 2 :(得分:2)

Uri uri = null;
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
  uri = data.getData();
}

答案 3 :(得分: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;
}