【问题标题】:FileProvider - Internal Storage - Failed to find configured root that containsFileProvider - 内部存储 - 找不到配置的根目录,其中包含
【发布时间】:2018-02-18 02:06:27
【问题描述】:

我知道 SO 有很多关于这个问题的问题。我尝试了大约 10 种不同的方法,但都无法正常工作。我总是得到下一个错误:

  java.lang.IllegalArgumentException: Failed to find configured root that contains /file:/storage/emulated/0/Android/data/app.kwork/files/IMG-afbfdc57c016fb1ff7dd983a056edffa-V.jpg

Official tutorial 也没有帮助。我将在下面分享我的代码,也许我做错了什么,你知道这里到底出了什么问题..

清单:

  <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="app.kwork.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

provider_paths.xml

<paths>
    <files-path path="." name="files" />
</paths>

我将文件保存到:

 File futureStudioIconFile = new File(getExternalFilesDir(null) + File.separator + fileName);

文件路径示例:file:///storage/emulated/0/Android/data/app.kwork/files/IMG-afbfdc57c016fb1ff7dd983a056edffa-V.jpg

并尝试与下一个代码共享此文件:

  public void openFile(String filePath){
        File file = new File(filePath);
        Log.d(TAG, "openFile: filePath: " + filePath);
        Uri photoURI = FileProvider.getUriForFile(context, "app.kwork.provider", file);

        MimeTypeMap myMime = MimeTypeMap.getSingleton();
        Intent newIntent = new Intent(Intent.ACTION_VIEW);
        String mimeType = myMime.getMimeTypeFromExtension(Utils.fileExt(filePath));
        newIntent.setDataAndType(photoURI,mimeType);
        newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        newIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        try {
            context.startActivity(newIntent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(context, "No handler for this type of file.", Toast.LENGTH_LONG).show();
        }

    }

我在 xml 中尝试了不同的权限和路径,但我现在在这里。

UPD:在 Android Oreo 上测试

【问题讨论】:

    标签: android android-fileprovider


    【解决方案1】:

    根据FileProvider documentation&lt;external-files-path&gt; 对应于getExternalFilesDir() - 您使用的&lt;files-path&gt; 对应于getFilesDir()

    您必须将您的paths.xml 更改为使用&lt;external-files-path&gt; 或将您的文件存储在getFilesDir() 中。

    【讨论】:

    • 正如我所说,我尝试了很多变体,包括你的。
    • 不行
    【解决方案2】:

    文件路径示例:file:///storage/emulated/0/Android/data/app.kwork/files/IMG-afbfdc57c016fb1ff7dd983a056edffa-V.jpg

    这不是文件路径。这是Uri

    文件路径为/storage/emulated/0/Android/data/app.kwork/files/IMG-afbfdc57c016fb1ff7dd983a056edffa-V.jpg

    不要将Uri 的字符串形式传递给File 构造函数。你最终会得到一个格式错误的File,而getUriForFile() 将无法处理那个格式错误的File

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 2013-12-14
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多