无法找到包含 /data/data/com.company.app/files

问题描述

不知道为什么我会得到这个,因为我认为我正确地遵循了示例 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。

谁能看到我做错了什么?

解决方法

好的……明白了。需要调整一些东西。首先代码应该是这样的

private fun shareMultipleVideos(names: List<String>,context: Context) {
    val uris: ArrayList<Uri> = ArrayList()

    for (name in names) {
        val videoFile = File(context.filesDir,name)
        Timber.i("videoFile ${videoFile.name} exists: ${videoFile.exists()}")
        val uri = FileProvider.getUriForFile(context.applicationContext,context.packageName + ".provider",videoFile)
        Timber.i("Adding Uri: $uri")
        uris.add(uri)
    }

    val intent = Intent()
    intent.action = Intent.ACTION_SEND_MULTIPLE
    intent.putExtra(Intent.EXTRA_SUBJECT,"Shared files")
    intent.type = "video/mp4"
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true)
    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uris)
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

    Timber.i("Intent: $intent")

    try{
        ContextCompat.startActivity(context,Intent.createChooser(intent,"Shared Videos"),null)
    } catch (e: Exception) {
        Timber.e("Exception starting activity. \nException was ${e.message}\n Stack trace to follow:\n ${e.stackTrace}")
    }
}

接下来,需要稍微调整路径 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="." />
</paths>