首页 > 解决方案 > IllegalArgumentException:未能找到包含在 FileProvider.getUriForFile 上的配置根

问题描述

我正在尝试从远程位置共享文件,但出现以下异常:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/31AB-1310/Android/data/com.example.myapp/cache/EasyImage/a9c926ea-4dce-44b7-94e9-a5dca6b91a5d-450242437.jpg
    at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
    at pl.aprilapps.easyphotopicker.EasyImage.createCameraPictureFile(EasyImage.java:54)
    at pl.aprilapps.easyphotopicker.EasyImage.createChooserIntent(EasyImage.java:109)

我的 FileProvider 代码具有以下清单条目:

<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_provider_paths" />
</provider>

我的 res/xml/filpaths.xml 文件:

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

我的 Activity.java 文件:

private void shareCurrentData(String filepath) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("*/*");
    ArrayList<Uri> files = new ArrayList<Uri>();
    if (intent.resolveActivity(getPackageManager()) != null) {
        try {
            File shareFile= new File(filepath);
            Uri shareUri =FileProvider.getUriForFile(this, AUTHORITY,shareFile);
            //files.add(textUri);
            files.add(shareUri);
        } catch (Exception ex) {
            System.out.println(ex);
        }
        intent.putExtra(Intent.EXTRA_STREAM, files);
        startActivity(Intent.createChooser(intent,"Share using....."));
    }
}

我只是不明白是什么引发了异常,因为一切似乎都合适。

标签: androidandroid-webview

解决方案


/storage/31AB-1310/Android/data/com.example.myapp/cache/EasyImage/a9c926ea-4dce-44b7-94e9-a5dca6b91a5d-450242437.jpg

这样的路径通常来自可移动存储。FileProvider不支持可移动存储。

因此,重点确保它filepath是在外部存储上,而不是可移动存储上。


推荐阅读