【问题标题】:Android image captured using camera doesn't get saved in specified custom folder on android nougat使用相机拍摄的 Android 图像不会保存在 android nougat 上的指定自定义文件夹中
【发布时间】:2017-02-20 15:31:33
【问题描述】:

我正在尝试使用 android 相机将图像保存在名为“appFolder”的文件夹中。我的目标 sdk 是 25。我的设备在 android nougat 上运行。但是,当我使用“dispatchTakePictureIntent()”单击图像时。图像没有保存在 appFolder 中。它保存在 DCIM/camera 文件夹中。为什么会发生这种情况以及如何将其保存在我的自定义文件夹中?

 private void dispatchTakePictureIntent() {
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                // Ensure that there's a camera activity to handle the intent
                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    // Create the File where the photo should go
                    File photoFile = null;
                    try {
                        photoFile = createImageFile();
                    } catch (IOException ex) {
                        // Error occurred while creating the File
                        Log.i("imageCaptutreError", ex.getMessage());

                    }
                    // Continue only if the File was successfully created
                    if (photoFile != null) {
                        Uri photoURI = FileProvider.getUriForFile(this,
                                "com.abc.def",
                                photoFile);
                        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                        startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                    }
                }
            }

    private File createImageFile() throws IOException {
            File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "appFolder");
            if (!folder.exists()) {
                folder.mkdir();
            }
            File tempFile = new File(folder, "temp_image.png");
                    /*new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "appFolder" + File.separator + "temp_image.png");*/

            mCurrentPhotoPath = tempFile.getAbsolutePath();
            return tempFile;
        }

Mainifest 中的提供者

   <provider
                android:name="android.support.v4.content.FileProvider"
                android:authorities="com.abc.def"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/file_paths"></meta-data>
            </provider>

@xml/file_paths

  <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-path name="my_images" path="appFolder/" />
    </paths>

【问题讨论】:

    标签: android android-camera android-camera-intent android-fileprovider android-7.1-nougat


    【解决方案1】:

    部分原因是您没有在Intent 上调用addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)。就目前而言,其他应用对您的Uri 标识的位置没有写入权限。

    但是,请记住,第三方相机应用存在错误。理想情况下,他们尊重EXTRA_OUTPUT。然而,有些人不会:

    • ...因为他们通常会忽略EXTRA_OUTPUT,或者
    • ...因为他们不知道如何处理EXTRA_OUTPUTUri 上的content 方案(甚至Google 自己的相机应用程序在2016 年年中之前也有这个问题)

    FWIW,this sample app 显示使用 ACTION_IMAGE_CAPTUREFileProvider

    【讨论】:

    • 添加行 takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); ....但没用
    • @PrateekRatnaker:然后相机应用程序决定将图像存储在它想要的位置,而不管EXTRA_OUTPUT。对此您无能为力。
    • 我可以在活动结果中获取存储图像的路径并将其复制到我想要的位置吗?
    • 请注意,data 位图通常很小;它的官方使命是为捕获的图像提供屏幕缩略图。即使在您的设备上您可能对其质量感到满意,您也应该了解其他相关设备的行为。
    • 如果您选择依赖在您的设备上实现 MediaStore.ACTION_IMAGE_CAPTURE 意图的应用程序,请知道这可能是 a) 一个很好的预装相机最适合特定设备的应用程序b) 不完全兼容的有缺陷的预装相机应用程序c) 最终用户在其设备上安装的第三方应用程序。您应该对它抱有最低限度的期望,并为它默默地忽略您提供的EXTRA_OUTPUT 的情况做好准备。或者,您可以为您的应用构建自定义相机捕捉活动。
    猜你喜欢
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 2012-01-14
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    相关资源
    最近更新 更多