【问题标题】:Permission Denial: reading androidx.core.content.FileProvider trying to share a video file权限拒绝:读取 androidx.core.content.FileProvider 尝试共享视频文件
【发布时间】:2021-06-05 19:50:09
【问题描述】:

我已经查看了几篇关于此的帖子,并且我已经尝试了六种建议的解决方案。我有一些与此工作几乎相同的东西,而不会在我编写的另一个应用程序中引发异常,但我只是无法让这个不引发异常,尽管文件确实被传输了!正在设置权限并且文件确实可以正常传输,但仍然会引发异常,尽管它不会使应用程序崩溃。

我拥有的是这样的:

fun shareVideo(videoFile: File, context: Context) {
  if(videoFile.exists()) {
    Timber.i("Video file exists and the length in bytes is: ${videoFile.length()}")
  } else {
    Timber.w("Video file does not exist. Exiting.")
    return
  }

  val uris = arrayListOf<Uri>()
  val uri = FileProvider.getUriForFile(context.applicationContext, context.packageName + ".provider", videoFile)

  Timber.i("Uri: $uri + path: ${uri.path}")

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

堆栈跟踪如下所示:

2021-03-07 14:21:59.570 6039-6207/com.company.app E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://com.company.app.provider/external_files/1615148514218.mp4 from pid=32136, uid=1000 requires the provider be exported, or grantUriPermission()
    at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:820)
    at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:684)
    at android.content.ContentProvider$Transport.query(ContentProvider.java:239)
    at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:106)
    at android.os.Binder.execTransactInternal(Binder.java:1154)
    at android.os.Binder.execTransact(Binder.java:1123)

Timber 日志输出如下所示:

2021-03-07 14:33:32.713 7396-7396/com.company.app I/StorageUtilsKt: Video file exists and the length in bytes is: 7511455
2021-03-07 14:33:32.719 7396-7396/com.company.app I/StorageUtilsKt: Uri: content://com.company.app.provider/external_files/1615149206171.mp4 + path: /external_files/1615149206171.mp4
2021-03-07 14:33:37.337 7396-7396/com.company.app I/StorageUtilsKt: Intent: Intent { act=android.intent.action.SEND_MULTIPLE typ=video/mp4 flg=0x10000001 (has extras) }
2021-03-07 14:33:38.604 7396-7589/com.company.app E/DatabaseUtils: Writing exception to parcel

所以文件就在那里,我在附件中看到它确实添加了它,我确实通过电子邮件/等获得了它。并且可以查看,但是在日志中抛出异常。

我的 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="." />
</paths>

我的清单中有这个:

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

一切看起来都与我的应用程序一模一样,但这个应用程序抛出了异常。

谁能看出我在做什么?

【问题讨论】:

  • 尝试暂时删除Intent.createChooser(),直接使用intent。如果可行,也许switch to setClipData() or ShareCompat.IntentBuilder
  • 完整路径在上面的日志中,但这里是:Uri: content://com.company.app.provider/external_files/1615149206171.mp4。同样,这确实有效,我可以在电子邮件/等中看到另一端的视频。但我不喜欢它抛出这个异常,因为我以前没见过。
  • 我看到的行为和你一样。您是否曾经修复它或看到不修复它的任何负面后果?

标签: android android-fileprovider


【解决方案1】:

在这里查看这个https://stackoverflow.com/a/59439316

您需要为可用于此意图的每个可用应用授予权限。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
  • Obrigado André deu certo!
猜你喜欢
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
  • 2019-11-12
  • 2021-06-23
  • 1970-01-01
  • 1970-01-01
  • 2011-12-23
  • 2012-05-31
相关资源
最近更新 更多