Android Inbuild(ActionImageCapture) Intent 返回空 Intent 。无法传递结果 {who= null}

标签 android nullpointerexception android-intent android-camera

我正在使用默认相机 Intent 在我的应用程序中获取图像。问题是相机在 onActivityResult() 上返回 null。 ResultCodeRequestCode 按预期返回。

我的 Intent 是:

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1224;
....
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

OnactivityResult 是:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
    //use imageUri here to access the image
    Uri imageuri = data.getData(); // here is getting crash 
    imageView.setImageFromUri(imageUri);
}
}
}

void setImageFromUri(Uri imgUri){
 ... TODO assign image from uri
}

当我输入日志时,我得到了 resultCode 和 responseCode 不为空

resultCode = -1
requestCode = 1224

我哪里做错了?

But the taken picture is stored in the path (imageUri) as I specified

有没有其他方法可以使用相机获取图像。

最佳答案

您似乎在 onActivityResult 之前知道 imageUri。这不是正确的答案,但可以正常工作。

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 

//此图片 uri 仅供您使用

所以不要用

 Uri imageuri = data.getData();

只需使用您知道的 uri。

您的代码如下所示:

if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
  if (resultCode == RESULT_OK) {
//use imageUri here to access the image
imageView.setImageFromUri(imageUri); // imageUri should be global in the activity
  }
}

关于Android Inbuild(ActionImageCapture) Intent 返回空 Intent 。无法传递结果 {who= null},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8246921/

相关文章:

android - 是否有像 DatePicker 这样的组件来选择 String 值?

android - 嵌套 ScrollView 在 CoordinatorLayout 中占用额外空间

android - 使用android intent在特定时间打开youtube视频

android - 广播接收器未通过服务接收 Intent

Android:如何制作我自己的 EventListener?

Android WallpaperManager 缩放位图太小

java - 为什么这段代码会抛出 NullPointerException?

java - 向 ActionBar 项目添加监听器,onOptionsItemSelected NullPointerException

java - 如何在 Android 应用程序中调试 Butterknife 引发的空指针异常?

Android:从 BroadcastReceiver 事件发送 LocalBroadcastManager 的 Intent