ACTION_IMAGE_CAPTURE returns 图像文件作为额外而不是数据

ACTION_IMAGE_CAPTURE returns imagefile as Extra instead of Data

我的应用程序的一个功能包括相机。用户拍照,应用程序显示图像。

我使用 StartActivityForResult 开始 ACTION_IMAGE_CAPTURE 的意图,并使用 onActivityResult 捕获响应。 通常 我可以根据在 onActivityResult 中收到的意图简单地调用 getData() 并获取 MediaProvider 的内容 uri。

我的问题如下:我的测试设备之一,华为 ALE-L21,我在 onActivityResult 中获得的意图没有数据,但它有一个 parcellable extra。我怎样才能从中获取用户照片? Android Studio 也没有告诉我 parcellable extra 的名称


免责声明:我调用 getActivity() 是因为我在片段中使用此代码


这是我用来拿相机的。

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
    startActivityForResult(takePictureIntent, getResources().getInteger(R.integer.RQ_PERMISSION_WRITE_EXTERNAL_STORAGE));
}

这是我的 onActivityResult 中的代码

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultIntent) {
    super.onActivityResult(requestCode, resultCode, resultIntent);

    switch (requestCode) {
        case 1: //RQ_ImageCapture
        case 2: //RQ_ImageSelected
            if (resultIntent != null) {
                try {
                    Uri selectedImage = resultIntent.getData();
                    if(selectedImage != null) {
                        Bitmap bitmap = 
                        MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
                        setImageAsBackground(selectedImage, bitmap);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            break;
    }
}

Usually I can simply call getData() on the intent I receive in onActivityResult and get the content uri for the MediaProvider.

您的代码将在其中许多设备上失败,包括所有最新的 Nexus 设备。 ACTION_IMAGE_CAPTURE 不应该 return 一个 Uri。引用 the documentation:

If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.

由于您没有包括 EXTRA_OUTPUT,您将获得 data 个带有缩略图的额外 (getData().getExtra("data"))。

the intent I get in onActivityResult has no data, but instead, it has a parcellable extra

鉴于您的 ACTION_IMAGE_CAPTURE 请求,这就是您应该得到的。

如果您想要全尺寸图像,请包含 EXTRA_OUTPUT,也许指向您应用中的 FileProvider,如我在 this sample app 中演示的那样。然后,您知道 Uri 照片应该放在哪里,因为您指定了 Uri.