【发布时间】:2016-10-06 12:07:43
【问题描述】:
在我的应用中使用FileProvider.getUriForFile 时仅在华为设备上发生了异常:
Exception: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/<card name>/Android/data/<app package>/files/.export/2016-10-06 13-22-33.pdf
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(SourceFile:711)
at android.support.v4.content.FileProvider.getUriForFile(SourceFile:400)
这是我的清单中文件提供程序的定义:
<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>
配置路径的资源文件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="external_files" path="" />
</paths>
您知道这个问题的原因以及为什么它只发生在华为设备上吗?鉴于我没有华为设备,我该如何调试?
更新:
我在我的应用程序中添加了更多日志,但在这些设备上同时打印 ContextCompat.getExternalFilesDirs 和 context.getExternalFilesDir 时得到了一些不一致的结果:
ContextCompat.getExternalFilesDirs:
/storage/emulated/0/Android/data/<package>/files
/storage/sdcard1/Android/data/<package>/files
context.getExternalFilesDir:
/storage/sdcard1/Android/data/<package>/files
这与声明 The first path returned is the same as getExternalFilesDir(String) 的 ContextCompat.getExternalFilesDirs 的文档不一致
这解释了这个问题,因为我在我的代码中使用了context.getExternalFilesDir,而FileProvider 使用了ContextCompat.getExternalFilesDirs。
【问题讨论】:
-
你从哪里/如何得到这个
File?/storage/<card name>看起来不正确。 -
请告知 getExternalStorageDirerctory() 在此设备上提供的内容。
-
我正在使用
Context.getExternalFileDir(null)获取文件。根据我在这些设备上的日志,它可以返回 storage/sdcard1/、/storage/3565-3131/、/storage/73A8-8626/、/storage/864A-F3ED...
标签: android android-fileprovider huawei-mobile-services