自动授予 Uri 权限
Uri permission granted automatically
我有一个简单的内容提供程序,可以向其他应用程序提供文件。它扩展了 ContentProvider
(不是 FileProvider
)并且在清单中声明如下:
<provider
android:name="com.FileProvider"
android:authorities="com.FileProvider"
android:exported="false"
android:grantUriPermissions="true" />
query
方法 returns 包含以下列(_display_name
、_data
和 size
)的简单游标。
我还实现了 openAssetFile
,其中 returns 同一个文件。
我正在使用以下代码与其他应用共享我的 URI:
val intent = Intent(Intent.ACTION_SEND)
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://com.FileProvider/teste"))
intent.type = "image/*"
startActivity(intent)
URI 中的最终路径段无关紧要,因为 query
方法 returns 总是相同的游标。
我成功地能够与其他应用程序共享文件,但是,我在任何地方都没有通过调用 Context.grantUriPermission
或通过意图标志授予对此 URI 的读取或写入权限。提供程序未导出。
我已经在使用 FileProvider
实现的其他应用程序中检查了此行为。即使 URI 指向内部文件,也可以共享它们。
删除 android:grantUriPermissions="true"
会产生预期的行为,所以这行似乎是自动授予权限的。但这不是文档所说的。
我的问题是,如果我没有授予其他应用程序这样做的权限,为什么我能够共享此 URI?
I'm successfully able to share the file with other apps, however, in no place am I giving read nor write permissions to this URI by calling Context.grantUriPermission nor through intent flags.
Google 添加了在某些情况下自动授予 FLAG_GRANT_READ_URI_PERMISSION
的逻辑,我认为这是向后兼容的举措。 ACTION_SEND
was one of them。我仍然引导开发人员手动添加标志,以防某些未来的 Android 版本恢复此更改(或设备制造商),但从技术上讲这不是必需的。
我有一个简单的内容提供程序,可以向其他应用程序提供文件。它扩展了 ContentProvider
(不是 FileProvider
)并且在清单中声明如下:
<provider
android:name="com.FileProvider"
android:authorities="com.FileProvider"
android:exported="false"
android:grantUriPermissions="true" />
query
方法 returns 包含以下列(_display_name
、_data
和 size
)的简单游标。
我还实现了 openAssetFile
,其中 returns 同一个文件。
我正在使用以下代码与其他应用共享我的 URI:
val intent = Intent(Intent.ACTION_SEND)
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://com.FileProvider/teste"))
intent.type = "image/*"
startActivity(intent)
URI 中的最终路径段无关紧要,因为 query
方法 returns 总是相同的游标。
我成功地能够与其他应用程序共享文件,但是,我在任何地方都没有通过调用 Context.grantUriPermission
或通过意图标志授予对此 URI 的读取或写入权限。提供程序未导出。
我已经在使用 FileProvider
实现的其他应用程序中检查了此行为。即使 URI 指向内部文件,也可以共享它们。
删除 android:grantUriPermissions="true"
会产生预期的行为,所以这行似乎是自动授予权限的。但这不是文档所说的。
我的问题是,如果我没有授予其他应用程序这样做的权限,为什么我能够共享此 URI?
I'm successfully able to share the file with other apps, however, in no place am I giving read nor write permissions to this URI by calling Context.grantUriPermission nor through intent flags.
Google 添加了在某些情况下自动授予 FLAG_GRANT_READ_URI_PERMISSION
的逻辑,我认为这是向后兼容的举措。 ACTION_SEND
was one of them。我仍然引导开发人员手动添加标志,以防某些未来的 Android 版本恢复此更改(或设备制造商),但从技术上讲这不是必需的。