无法从 FileProvider 保存文件

Unable to save file from FileProvider

我正在从 android 应用程序捕获图像并使用 Fileprovider 将图像保存在外部存储器中。但它给了我一个例外

java.lang.IllegalArgumentException: 
Failed to find configured root that contains /data/data/com.rocketstove/files/SAMS/JMC-R-1256655/application_form_first.jpg

我在 androidManifest 中配置了这样的提供程序

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

我还在 file_paths

中添加了以下行
<?xml version="1.0" encoding="utf-8"?>
<paths>
  <external-path
    name="external_files"
    path="." />
</paths>

在java代码中

if (Build.VERSION.SDK_INT >= 23) {
        File imagePath = new File(getActivity().getFilesDir(), "SAMS");
        if (!imagePath.exists()) {
            imagePath.mkdir();
        }
        imagePath = new File(getActivity().getFilesDir() + "/SAMS", rocketId);
        if (!imagePath.exists()) {
            imagePath.mkdir();
        }
        File imageFile = new File(imagePath.getPath(), filename);
        intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        String authority = getActivity().getApplicationContext().getPackageName() + ".fileprovider";
        Uri uri = FileProvider.getUriForFile(getActivity(), authority, imageFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivity(intent);
}

编辑 file_paths

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path name="external_files" path="Android/data/com.rocketstove.files/"/>
</paths>

Java代码

if (Build.VERSION.SDK_INT >= 23) {
        File file
                 =getActivity().getExternalFilesDir(null);
        File imagePath = new File(file , rocketId);
        if (!imagePath.exists()) {
            imagePath.mkdir();
        }
        File imageFile = new File(imagePath.getPath(), filename);
        intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        String authority = getActivity().getApplicationContext().getPackageName() + ".fileprovider";
        Uri uri = FileProvider.getUriForFile(getActivity(), authority, imageFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivity(intent);
    }

错误

ava.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.rocketstove/files/JMC-R-4555555/application_form_first.jpg
                  at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:719)
                  at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:404)

I have also add following line in file_paths

你有 external-pathdocumentation 表示:

Represents files in the root of the external storage area. The root path of this subdirectory is the same as the value returned by Environment.getExternalStorageDirectory().

那不是你的 File 指向的地方。

external-path 更改为 files-path,因为您的 Java 代码使用 getFilesDir()