【发布时间】:2021-02-03 03:34:22
【问题描述】:
不知道为什么我会得到这个,因为我认为我正确地遵循了示例 here。
许多其他人就同一件事提出了问题,但在查看了建议后,它仍然被打破。
完整的异常如下所示:
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.company.app/files/app_videos/VIDEO_20210202_184514.mp4
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
清单如下所示:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
provider_paths.xml 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path name="cache" path="/"/>
<external-cache-path name="external_cache" path="." />
<external-path name="external" path="." />
<external-files-path name="external_files" path="." />
<files-path name="app_videos" path="files/" />
</paths>
我正在使用的代码(几乎是从 Google 网站剪切和粘贴的)如下所示:
val imagePath = File(context.filesDir, "app_videos")
Timber.i("imagePath: ${imagePath.name}")
val newFile = File(imagePath, name)
Timber.i("newFile exists: ${newFile.exists()} length: ${newFile.length()}")
val uri = FileProvider.getUriForFile(context.applicationContext, context.packageName + ".provider", newFile)
Timber.i("Adding Uri: $uri")
最后一行永远不会执行,因为崩溃发生在它的前一行。
现在,我知道我在此处保存了文件: /data/user/0/com.company.app/files/VIDEO_20210202_/data/user/0/com.standardandroid.stalkersport/files/VIDEO_20210202_200455.mp4
我知道它在那里,因为我可以使用其他代码很好地播放它
所以,我知道该文件存在。我只是无法为它创建一个 Uri。
谁能看看我做错了什么?
【问题讨论】:
-
/data/user/0/com.company.app/files/VIDEO_20210202_/data/user/0/com.standardandroid.stalkersport/files/VIDEO_20210202_200455.mp4那是很多子目录。为什么又是 data/user/0 等等?为什么不只是/data/user/0/com.company.app/files/VIDEO_20210202_/VIDEO_20210202_200455.mp4?
标签: android android-fileprovider