【问题标题】:Unable to save file from FileProvider无法从 FileProvider 保存文件
【发布时间】:2018-03-10 15:19:37
【问题描述】:

我正在从 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);
}

编辑 文件路径

<?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)

【问题讨论】:

    标签: android android-6.0-marshmallow android-7.0-nougat


    【解决方案1】:

    我还在 file_paths 中添加了以下行

    你有external-pathdocumentation 指出:

    表示外部存储区域根目录中的文件。此子目录的根路径与 Environment.getExternalStorageDirectory() 返回的值相同。

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

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

    【讨论】:

    • 但我需要存储在外部存储上。
    • @BibekShakya:那就不要使用getFilesDir()。理想情况下,使用getExternalFilesDir(),然后将external-path 更改为external-files-path
    • @BibekShakya:应该是抱怨文件路径不同。否则,您没有更改 Java 代码。
    • 它正在使用 java.lang.IllegalArgumentException:无法找到包含 /storage/emulated/0/Android 文件路径的已配置根目录,该文件路径是内部存储
    • 您可以同时使用外部路径和外部文件路径。正如函数名告诉你的,getExternalFilesDir() 提供外部存储。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多