android - ACTION_IMAGE_CAPTURE 将图像文件返回为 Extra 而不是 Data

标签 android android-intent camera android-5.0-lollipop huawei-mobile-services

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

我使用 StartActivityForResult 通过 ACTION_IMAGE_CAPTURE 启动 Intent ,并使用 onActivityResult 捕获响应。 通常我可以简单地对在 onActivityResult 中收到的 Intent 调用 getData() 并获取 MediaProvider 的内容 uri。

我的问题如下:我的一台测试设备,华为 ALE-L21,我在 onActivityResult 中获得的 Intent 没有数据,但它有一个可分割的额外数据。我怎样才能从中获取用户照片? Android Studio 也不告诉我可分割额外的名称


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


这是我用来获取相机的。

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 不应返回 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

关于android - ACTION_IMAGE_CAPTURE 将图像文件返回为 Extra 而不是 Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38851665/

相关文章:

ios - 从 CosmicMind-Material 捕获 API : Camera preview not correct

android - 在Android上获取图像的文件路径

Android - 动态请求相机访问

Android 从拇指到中心缩放图像

java - "Unfortunately ' 应用程序 ' has stopped"错误

android - 通过 facebook 为 android jelly bean 分享 Intent

java - 从 android 中的服务接收广播时出现 NULL 对象引用错误

android - 如何制作一个像样的用户界面

android - 如何从 "/proc/cpuinfo"文件获取Android设备的具体信息?

java - 错误 java.lang.RuntimeException : Unable to start activity