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

发布于 2024-10-18 10:32:28 字数 265 浏览 1 评论 0原文

我有这个代码:

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦初启 2024-10-25 10:32:28
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    Uri u = 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.

长梦不多时 2024-10-25 10:32:28

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

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 的流,并且应该全部设置完毕。

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.

只有一腔孤勇 2024-10-25 10:32:28
Uri uri = null;
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
  uri = data.getData();
}
Uri uri = null;
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
  uri = data.getData();
}
可爱咩 2024-10-25 10:32:28
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;
}
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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文