I using FileProvider to generate an uri, then I want to pass it to BroadcastReceiver in another App, but I only get Exception of "Permission Denial: opening provider", how can I fix it?
There's two app: Sender & Receiver, then Sender want to share a file to Receiver, but Receiver don't need an UI, it process file in background, so I use Broadcast rather than Activity to do that. But I only get "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>
file_path_config.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external" path="."/>
</paths>
sendBroadcast
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>
MyReceiver
override fun onReceive(context: Context, intent: Intent) {
intent.data?.apply {
val inputStream = context.contentResolver.openInputStream(this)
Log.i(TAG, "onReceive: $inputStream")
inputStream?.close()
}
}
I try it many times, but only get Caused by: java.lang.SecurityException: Permission Denial: opening provider androidx.core.content.FileProvider from ProcessRecord and then crash.
But when I using Activity instead of Broadcast, it work fine.
FileProvider documentation mentions that the lifetime of the permission is bound to the lifetime of the receiving Activity or Service. It says nothing about Broadcast receivers. So I suppose they cannot manage such intents without gettting this SecurityException.