gpt4 book ai didi

android - 为什么 FileProvider 不能与 Broadcast 一起使用?

转载 作者:行者123 更新时间:2023-11-29 02:22:33 27 4
gpt4 key购买 nike

我使用 FileProvider 生成一个 uri,然后我想将它传递给另一个 App 中的 BroadcastReceiver,但我只得到 Exception of "Permission Denial: opening provider",我该如何解决?

有两个应用程序:Sender 和 Receiver,然后 Sender 想将文件共享给 Receiver,但是 Receiver 不需要 UI,它在后台处理文件,所以我使用 Broadcast 而不是 Activity要做到这一点。但我只得到 "Permission Denial"

发件人

显现
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.sender"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path_config"/>
</provider>

文件路径配置文件
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external" path="."/>
</paths>
发送广播

var intent = Intent().apply {
action = "com.action"
component = ComponentName(
"com.receiver",
"com.receiver.MyReceiver"
)
}

intent.data = FileProvider.getUriForFile(
this,
"com.sender",
File("/storage/emulated/0/Cmd/zxz")
)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)

grantUriPermission("com.receiver", intent.data, Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)

sendBroadcast(intent)

接收者

显现

<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.action"/>
<data
android:mimeType="*/*"
android:scheme="content"/>
</intent-filter>
</receiver>

我的收件人

override fun onReceive(context: Context, intent: Intent) {
intent.data?.apply {
val inputStream = context.contentResolver.openInputStream(this)
Log.i(TAG, "onReceive: $inputStream")
inputStream?.close()
}
}

我尝试了很多次,但只得到 Caused by: java.lang.SecurityException: Permission Denial: opening provider androidx.core.content.FileProvider from ProcessRecord 然后崩溃。

但是当我使用 Activity 而不是 Broadcast 时,它工作正常。

最佳答案

FileProvider 文档提到权限的生命周期绑定(bind)到接收 Activity 或服务的生命周期。它没有提到广播接收器。所以我想他们无法在不获取此 SecurityException 的情况下管理此类 Intent 。

关于android - 为什么 FileProvider 不能与 Broadcast 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54395256/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com