【问题标题】:Java.Lang.IllegalArgumentException using FileProviderJava.Lang.IllegalArgumentException 使用 FileProvider
【发布时间】:2017-05-09 02:03:33
【问题描述】:

我在尝试使用 FileProvider 时遇到此异常:

Java.Lang.IllegalArgumentException: Unable to find configured root for content://com.abc/Android/data/com.abc/files/345345345.pdf   Java.Lang.IllegalArgumentException

我猜我的 file_paths.xml 用错误的路径配置了 FileProvider。我的代码:

file_paths.xml:

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

androidManifest.xml

<application android:label="@string/AppName" android:icon="@drawable/ic_launcher">
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
    <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.abc" android:exported="false" android:grantUriPermissions="true">
        <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
    </provider>
</application>

我应该在file_paths.xml 中输入pathname 中的哪些值,以使用内容URI:content://com.abc/Android/data/com.abc/files/345345345.pdf 公开文件?

我创建 URI 的代码:

File newFile = new File (Application.Context.FilesDir.AbsolutePath, filename);
                Android.Net.Uri androidContentUri = FileProvider.GetUriForFile (Application.Context, Application.Context.PackageName, newFile);
                System.Uri systemContentUri = SystemUri (androidContentUri);
                return systemContentUri;

【问题讨论】:

    标签: android xamarin xamarin.android android-fileprovider


    【解决方案1】:

    您的参赛作品:

    <files-path path="com.abc/Android/data/com.abc/files/" name="files" />
    

    应该变成:

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

    Path 在这种情况下表示应用程序内部存储区域的files/ 子目录中的文件。

    此应用私有子目录与返回值相同:

    Application.Context.FilesDir;
    

    所以你的应用中的文件位置是:

    var path = Path.Combine(Application.Context.FilesDir.AbsolutePath, "files");
    

    【讨论】:

    • 谢谢!我将&lt;files-path 更改为您的建议,并尝试将我的代码路径(您答案的底线)放入我的代码中。 AbsolutePath 的结果中已经有“文件”,所以我没有使用 Path.Combine。但我现在收到此错误:Java.Lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.abc/files/2534534.pdf。这是我创建文件的代码:File newFile = new File (Application.Context.FilesDir.AbsolutePath, filename);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多