安卓文件提供者 : Failed to find configured root that contains

标签 android android-fileprovider

我创建了一个应用程序,可以创建 gpx 文件。除共享外,一切正常。 因此我创建了一个文件提供程序。你可以在下面看到它的配置。 Provider 在我运行 Android 8.0.0 的 Android 设备上运行良好,但在 friend 华为 (6.0) 上它无法运行

Fatal Exception: java.lang.IllegalArgumentException
Failed to find configured root that contains /storage/8737-15E4/Android/data/XXX/cache/20171009_171900.gpx

list 中的提供者:

<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>

文件路径.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>

代码中的用法:

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);

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

最佳答案

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

Uri gpxContentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", gpxFile);

用这个:

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);
}

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

关于安卓文件提供者 : Failed to find configured root that contains,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46673683/

相关文章:

android - 在 Android 应用程序中添加多个文件提供程序

java - getFragmentManager().findFragmentById 返回 null

java - API 21 如何获取选择的时间?

c# - Android 与 PC 的连接

java - 无法使用 FileProvider 和外部 PDF 编辑器保存 PDF 文件

android - 使用 FileProvider 共享缓存图像

android - 在 android 中进行 Stripe 集成并创建 token

java - 我可以防止我的应用程序不能被其他应用程序杀死吗?

android - 如何从 whatsapp fileprovider 获取 txt 文件

android - FileProvider 不使用相机