android - onActivityResult 为图像捕获返回空数据

标签 android onactivityresult

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    filePath = getOutputMediaFile(FileColumns.MEDIA_TYPE_IMAGE);
    File file = new File(filePath);
    Uri output = Uri.fromFile(file);
    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    i.putExtra(MediaStore.EXTRA_OUTPUT, output);
    startActivityForResult(i, RETURN_FILE_PATH);
}

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    //data is always null here.
    //requestCode = RETURN_FILE_PATH;
    //resultCode = Activity.RESULT_OK;
}

我检查了 fileoutput Uri 的值,两者都很好,捕获的图像确实存在于该位置

但是 onActivityResult 中返回的数据始终为 null,即使在捕获图像之后也是如此。

编辑:

我检查了这个问题:

onActivityResult returns with data = null

上面写着:

Whenever you save an image by passing EXTRAOUTPUT with camera intent the data parameter inside the onActivityResult always return null. So, instead of using data to retrieve the image , use the filepath to retrieve the Bitmap.

也许这个解决方案对我有用。但是到目前为止,我的上述代码是相同场景的工作代码。

最佳答案

根据这个post预插入 uri 时数据为空。这意味着您已经在此处定义了输出 uri:

  i.putExtra(MediaStore.EXTRA_OUTPUT, output);

所以当你得到一个Activity.RESULT_OK;只需通过其已知的 url 加载拍摄的照片。

关于android - onActivityResult 为图像捕获返回空数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17827460/

相关文章:

android - 为结果启动 Activity 不返回调用 Activity

java - 从 onActivityResult() 调用时,notifyDataSetChanged() 不适用于我的 RecyclerView.Adapter

android - 如何从图库中获取图像

java - 无法生成固定大小的字母数字符号字符串

android - 如何在android中通过指定的Pincode获取位置/城镇和州?

android - 仅针对 nativeGetStyleAttribute 在 Samsung Galaxy S21+ 上获取 NullpointerException

android - KitKat 及以下版本的 VectorDrawableCompat Resources$NotFoundException

android - 设置时间表布局

安卓 : how Custom Dialog returns intent result to parent activity

java - 将数据从 fragment 发送到另一个 Activity 时不会调用 OnActivityResult