【问题标题】:java.lang.IllegalArgumentException: Failed to find configured root that containsjava.lang.IllegalArgumentException:找不到配置的根目录包含
【发布时间】:2018-08-27 08:08:16
【问题描述】:

这是我的代码块:

notificationIntent.setDataAndType(FileProvider.getUriForFile(
            context,
            "TrySomething",new File(context.getFilesDir().getAbsolutePath() + "/update_file")
    ),ANDROID_PACKAGE);

这是我的 XML 文件:

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

但我得到这个错误:

java.lang.IllegalArgumentException:找不到配置的根目录 包含 /data/data/com.example.trysomething/files/update_file

我该如何解决?

【问题讨论】:

  • 看看这个如何实现文件提供者。 stackoverflow.com/questions/50794378/…
  • 我试过了,先生。但我没有成功..
  • 您是否按照每个步骤进行操作?因为我看到您的 XML 文件看起来不像必需的 XML。请检查一下
  • 是的,先生,代码是否相同?我的错误是什么告诉我代码块是否错误。
  • 请不要破坏您的帖子。通过在 Stack Exchange 网络上发布,您已授予 SE 分发该内容的不可撤销的权利(在 CC BY-SA 3.0 license 下)。根据 SE 政策,任何破坏行为都将被撤销。

标签: android android-8.0-oreo android-fileprovider


【解决方案1】:

首先在 src->xml 文件夹中创建 Xml 文件 例如命名为file_paths_public.xml

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

然后将此代码添加到您的清单文件中

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

现在创建一个同时支持 api 26 及以下设备的函数 返回一些有效的uri

 public static Uri getUriFromFile(Context theCtx, File theSrcPath) {
        Uri requirdUri;

        // Above Compile SDKversion: 25 -- Uri.fromFile Not working
        // So we have to use Provider

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            requirdUri = FileProvider.getUriForFile(theCtx,
                    theCtx.getApplicationContext().getPackageName() + ".provider",
                    theSrcPath);
        } else {
            requirdUri = Uri.fromFile(theSrcPath);
        }

        return requirdUri;
    }

现在随时使用这个 uri 喜欢

 File FilePath = Environment.getExternalStorageDirectory().toString()/update_file/<here your file name >
   Uri shareImageUri = getUriFromFile(this, <your full file Path Add hear>)

【讨论】:

    【解决方案2】:
    1. 检查你的清单中是否有这个

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

      <?xml version="1.0" encoding="utf-8"?>
      <paths>
      <external-path name="directory_name" path="."/>
      </paths>
      
    3. 获取文件 URI 为

      Uri fileUri = FileProvider.getUriForFile(context, getApplicationContext().getPackageName() + ".provider", fileName(FILE))
      

    【讨论】:

    • 第 3 步 getUriForFile 函数想要一个文件而不是文件名。当我得到这样的文件时 new File(Environment.getExternalStorageDirectory() + "/update_file");我得到了一些错误
    • 文件保存在哪里
    • 当您尝试保存文件时...保存该文件实例并尝试将其传递给 getUriForFile 函数
    猜你喜欢
    • 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
    相关资源
    最近更新 更多