Android的Inbuild(ActionImageCapture)意图返回null意向.Cannot交付结果{谁= NULL}意图、意向、结果、Android

2023-09-06 10:41:33 作者:劳资真想跳ai(崖)

我使用的是默认的摄像头故意让我的应用程序中的图像。现在的问题是相机 onActivityResult返回null()。该结果code 请求code 正在回归正常。

I am using the default camera intent to get the image in my app. The problem is camera returns null on onActivityResult() . The ResultCode and RequestCode are returning as expected.

我的意图调用是:

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是:

OnactivityResult is:

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
}

当我把登录我得到的结果code和响应code是不为空

As I put Log I got the resultCode and responseCode are not null

resultCode = -1
requestCode = 1224

在哪里我做的错误?

Where I am doing mistake?

但是拍摄图像被存储在路径(imageUri),为我指定的

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

有没有任何其他方式使用相机来获得图像。

Is there any other way to get image using camera.

推荐答案

看来你知道onActivityResult前imageUri。这是不正确的答案,但将正常工作。

Seems you know the imageUri before onActivityResult. This is not correct answer but will work fine.

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 

//此图片URI只有你会使用

// This image uri only you're going to use

所以,不要使用

 Uri imageuri = data.getData();

只需使用你所知的URI。

just use the uri you known.

您code是这样的:

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
  }
}