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

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,但我不知道这个错误消息试图告诉我什么。如果你想要更多信息/代码,请在评论中告诉我。


当前回答

我做了什么来解决这个问题

AndroidManifest.xml

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

fileppaths .xml(允许FileProvider共享应用程序外部文件目录中的所有文件)

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

并在Java类-

Uri fileProvider = FileProvider.getUriForFile(getContext(),"com.mydomain.fileprovider",newFile);

其他回答

我做了什么来解决这个问题

AndroidManifest.xml

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

fileppaths .xml(允许FileProvider共享应用程序外部文件目录中的所有文件)

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

并在Java类-

Uri fileProvider = FileProvider.getUriForFile(getContext(),"com.mydomain.fileprovider",newFile);

你需要确保file_paths.xml文件的内容包含这个字符串=> "/Android/data/com.example.marek.myapplication/files/Pictures/"

从错误消息中,这是存储图片的路径。见预期样本

files_path.xml如下:

<external-path name="qit_images" path="Android/data/com.example.marek.myapplication/files/Pictures/" />

请注意,external-path不是指向二级存储,即“可移动存储”(尽管名称为“external”)。如果你得到“未能找到配置的根”,你可以将这一行添加到你的XML文件中。

<root-path name="root" path="." />

更多细节请参见FileProvider和次要外部存储

这对我也很管用。而不是给完整的路径,我给了path="Pictures",它工作得很好。

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

您的文件存储在getExternalFilesDir()下。它映射到<external-files-path>,而不是<files-path>。此外,您的文件路径中不包含图像/,因此XML中的path属性无效。

将res/xml/file_paths.xml替换为:

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

更新 2020 3月 13

特定路径的提供者路径如下:

<files-path/>——> <cache-path/>——> <external-path/>——> Environment.getExternalStorageDirectory() <external-files-path/>——> Context.getExternalFilesDir(String) <external-cache-path/>——> Context.getExternalCacheDir() <external-media-path/>——> Context.getExternalMediaDirs()

裁判:https://developer.android.com/reference/androidx/core/content/FileProvider