【问题标题】:Android Fileprovider: Failed to find configured root that containsAndroid Fileprovider:找不到配置的根目录包含
【发布时间】:2017-10-10 18:37:03
【问题描述】:

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

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

清单中的提供者:

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

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

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

【问题讨论】:

  • 存储权限设置如下:
  • 该文件系统路径看起来更像可移动存储而不是外部存储。但是,我希望FileProvider 和您自己的代码都能为getExternalCacheDir() 获得相同的奇怪值,因此它们应该是一致的。如果 FileProvider 以某种方式获得了 getExternalCacheDir() 的更传统的值(例如 /storage/emulated/0/Android/data/XXX/cache/20171009_171900.gpx),而您的代码却不是,那么您的症状将适合。
  • 好的。那么我应该扩展文件提供程序路径吗?
  • FileProvider 不支持可移动存储作为&lt;external-cache-dir&gt; 之类的替代品。我只是无法解释为什么FileProvider 显然可能会为getExternalCacheDir() 获得一个值,以及为什么您的代码会获得另一个值。您的代码中的 context 到底是什么?这个Context 来自哪里?
  • 是的——事实上,如果你使用其他东西,我会建议你这样做。我无法解释症状。一种解决方法是捕获异常并在 Android 6.0 和更早版本的设备上使用 Uri.forFile()

标签: android android-fileprovider


【解决方案1】:

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

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 的“华为 P8 Lite (PRA-LX1)”上,而 Mojo 说它只发生在他朋友的华为 (6.0) 上。我开始认为这只是这些手机的问题,但最好有一个解决方法。

【讨论】:

  • uriList 是什么?
  • @EpicPandaForce 首先,很棒的名字,其次,感谢您的关注! uriList 不应该在那里,但是,如果有人感兴趣,我个人使用此代码:ArrayList&lt;Uri&gt; uriList = new ArrayList&lt;Uri&gt;(); 然后我将其发送到发送意图 (ACTION_SEND_MULTIPLE) 我已经更正了答案,再次感谢!
猜你喜欢
  • 1970-01-01
  • 2013-12-14
  • 2017-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
相关资源
最近更新 更多