Android Fileprovider:无法找到包含的配置根目录

Moj*_*ojo 5 android android-fileprovider

我创建了一个应用程序,用于创建 gpx 文件。除了共享之外,一切正常。因此我创建了一个文件提供程序。您可以在下面看到它的配置。Provider 在我运行 Android 8.0.0 的 android 设备上运行良好,但在朋友 Huawei (6.0) 上它不工作

Fatal Exception: java.lang.IllegalArgumentException
Failed to find configured root that contains /storage/8737-15E4/Android/data/XXX/cache/20171009_171900.gpx
Run Code Online (Sandbox Code Playgroud)

清单中的提供者:

<provider
        android:name=".GenericFileProvider"
        android:authorities="com.package.test.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>
Run Code Online (Sandbox Code Playgroud)

文件路径.xml:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="external-files" path="/" />
<external-cache-path name="cache-files" path="/" />
</paths>
Run Code Online (Sandbox Code Playgroud)

代码中的用法:

File gpxFile = new File(context.getExternalCacheDir(), "20171009_171900.gpx");    
Uri gpxContentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", gpxFile);

        Intent gpxIntent = new Intent(Intent.ACTION_SEND);
        gpxIntent.setType("text/gpx");

        gpxIntent.putExtra(Intent.EXTRA_STREAM, gpxContentUri);

        Intent programChooser = Intent.createChooser(gpxIntent, context.getString(R.string.select_app_to_share));
        programChooser.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

        activityForDialog.startActivity(programChooser);
Run Code Online (Sandbox Code Playgroud)

我希望有人能帮我找到导致应用程序在某些设备上崩溃的错误...

Jar*_*red 4

修改您的“代码中的用法:”并替换第二行

Uri gpxContentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", gpxFile);
Run Code Online (Sandbox Code Playgroud)

有了这个:

Uri gpxContentUri;
try {
    gpxContentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", gpxFile);
} catch (IllegalArgumentException e) {
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());
    gpxContentUri = Uri.fromFile(gpxFile);
}
Run Code Online (Sandbox Code Playgroud)

注意:这个错误似乎只发生在我运行Android 7.0的“华为P8 Lite(PRA-LX1)”上,Mojo说这只发生在他朋友的华为(6.0)上。我开始认为这只是这些手机的问题,但最好有一个解决方法。