我试图用相机拍照,但我得到以下错误:

FATAL EXCEPTION: main
Process: com.example.marek.myapplication, PID: 6747
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.marek.myapplication/files/Pictures/JPEG_20170228_175633_470124220.jpg
    at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
    at com.example.marek.myapplication.MainActivity.dispatchTakePictureIntent(MainActivity.java:56)
    at com.example.marek.myapplication.MainActivity.access$100(MainActivity.java:22)
    at com.example.marek.myapplication.MainActivity$1.onClick(MainActivity.java:35)

AndroidManifest.xml:

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.marek.myapplication.fileprovider"
        android:enabled="true"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
</provider>

Java:

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) {
            Toast.makeText(getApplicationContext(), "Error while saving picture.", Toast.LENGTH_LONG).show();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.marek.myapplication.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }

file_paths.xml

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

我一整天都在搜索这个错误,试图理解FileProvider,但我不知道这个错误消息试图告诉我什么。如果你想要更多信息/代码,请在评论中告诉我。


当前回答

这可以解决每个人的问题: 所有标签都被添加,所以你不需要担心文件夹路径。 将res/xml/file_paths.xml替换为:

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

编辑时间:2021年6月1日

我们应该只使用我们需要的特定路径。 尝试自己的替代路径,使用你需要的。

有关更多信息,请参阅公认答案:https://stackoverflow.com/a/42516202/4498813

其他回答

Xamarin的。Android用户

这也可能是没有更新你的支持包时,针对Android 7.1, 8.0+。将它们更新到v25.4.0.2+,这个特定的错误可能就会消失(就像其他人所说的那样,您已经正确配置了file_path文件)。


背景介绍:我把目标从牛轧糖换成了奥利奥 Xamarin的。form应用和用Xam.Plugin.Media拍照 开始失败,出现上述错误消息,因此更新包 帮我做了这个戏法。

我发现至少您没有在file_paths.xml中提供与其他文件相同的路径。 所以请确保在3个地方提供完全相同的包名或路径,包括:

Android:权限属性在manifest中 file_paths.xml中的Path属性 在调用FileProvider.getUriForFile()时参数。

试试这个

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

问题可能不仅仅是路径xml。

以下是我的解决方案:

查看android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile()中的根课程:

    public File getFileForUri(Uri uri) {
        String path = uri.getEncodedPath();

        final int splitIndex = path.indexOf('/', 1);
        final String tag = Uri.decode(path.substring(1, splitIndex));
        path = Uri.decode(path.substring(splitIndex + 1));

        final File root = mRoots.get(tag); // mRoots is parsed from path xml
        if (root == null) {
            throw new IllegalArgumentException("Unable to find configured root for " + uri);
        }

        // ...
    }

这意味着mRoots应该包含所请求uri的标记。所以我写了一些代码来打印mRoots和uri的标签,然后很容易发现标签不匹配。

结果是将提供者权限设置为${applicationID}。提供者是个愚蠢的想法!这个权限非常常见,可能会被其他提供者使用,这会弄乱路径配置!

如果你使用内部缓存,那么使用。

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="cache" path="/" />
</paths>