使用FileProvider加载照片,导致java.lang.IllegalArgumentException:无法找到包含以下内容的已配置根目录

时间:2019-06-20 05:59:29

标签: android kotlin android-camera android-fileprovider

我想使用FileProvider从相机获取照片。我完成了本教程中的所有操作,但使用authority参数导致了错误。

教程:YouTube's Playlist Documentation

AndroidManifest内部的提供者:

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>

provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external_files" path="."/>
</paths>

更新:

这是创建文件的功能:

private fun createImageFile(): File {
        val timeStamp = SimpleDateFormat(
            "yyyyMMdd_HHmmss",
            Locale.getDefault()
        ).format(Date())
        val imageFileName = "IMG_" + timeStamp + "_"
        val image = File.createTempFile(
            imageFileName, /* prefix */
            ".jpg", /* suffix */
            null      /* directory */
        )

        return image
    }

这是provider_paths.xml(我不需要外部路径,因为我不希望用户接受存储权限以使用相机):

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path
        name = "name"
        path = "path" />
</paths>

这是打开的摄像头功能:

private fun openCamera() {
        val tempFile = createImageFile()
        photoUri = Uri.fromFile(tempFile)
        photoUri = FileProvider.getUriForFile(a, a.applicationContext.packageName + ".fileprovider", tempFile)

        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
        if (intent.resolveActivity(a.packageManager) != null) {
            a.startActivityForResult(intent, REQUEST_CAMERA_NODE_MISUSE)
        }
    }

错误:Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/my.package.name/cache/IMG_20190620_102915_7513574861671405500.jpg

1 个答案:

答案 0 :(得分:0)

确保将文件提供者也放在清单中的application标记内

<application
            ...>
     ....
    <provider
            android:authorities = "your_string"
            android:name = "androidx.core.content.FileProvider"
            android:exported = "false"
            android:grantUriPermissions = "true">
        <meta-data
                android:resource = "@xml/file_path"
                android:name = "android.support.FILE_PROVIDER_PATHS" />
    </provider>
</application>

我的file_path.xml就是这样

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
            name = "my_images"
            path = "Android/data/com.your.package/files/Pictures" />
</paths>

从Java代码

val authorities = "your_string"

val imageUri = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP)
     FileProvider.getUriForFile(context!!, authorities, imageFile)
else
     Uri.fromFile(imageFile)

callCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
            startActivityForResult(
                callCameraIntent,
                CAMERA_REQUEST_CODE
            )

更新

清单中的文件提供者权限与代码中的权限应匹配。 您的问题是您的authorities的清单文件和Java代码不同

`android:authorities = "${applicationId}.fileprovider"`

在您的Java代码中替换为

photoUri = FileProvider.getUriForFile(a, a.applicationContext.packageName + ".provider", tempFile)

对此

photoUri = FileProvider.getUriForFile(a, a.applicationContext.packageName + ".fileprovider", tempFile)

第二次更新

在此函数中,您将传递到目录,而不是这种情况:

private fun createImageFile(): File {
        val timeStamp = SimpleDateFormat(
            "yyyyMMdd_HHmmss",
            Locale.getDefault()
        ).format(Date())
        val imageFileName = "IMG_" + timeStamp + "_"
        val image = File.createTempFile(
            imageFileName, /* prefix */
            ".jpg", /* suffix */
            null      /* directory */
        )

        return image
    }

尝试

private fun createImageFile(): File {
            val timeStamp = SimpleDateFormat(
                "yyyyMMdd_HHmmss",
                Locale.getDefault()
            ).format(Date())
            val imageFileName = "IMG_" + timeStamp + "_"
            val image = File.createTempFile(
                imageFileName, /* prefix */
                ".jpg", /* suffix */
                context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) //null /* directory */
            )

            return image
        }