我们正在构建一个测验即时应用程序,用户可以在其中完成测验,然后共享他们的结果。我们通过链接共享一些文本,还显示了显示用户测验结果的图像。在已安装的应用程序中进行此流程时没有问题,但是在即时应用程序中,图像无法共享。
这是我们产生意图的方式:
val uri = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", image)
val shareIntent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, content)
putExtra(Intent.EXTRA_STREAM, uri)
type = "image/*"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
activity?.startActivity(Intent.createChooser(shareIntent, getString(R.string.quiz_share_title)))
这是我们基本应用清单中的提供者:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/fileprovider" />
</provider>
当用户在即时应用中共享图像时,此错误消息出现在logcat中:
java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://com.redacted.fileprovider/shared/1563809004297.png from pid=29184, uid=1000 requires the provider be exported, or grantUriPermission()
我尝试设置exported =“ true”,这会在启动时使即时应用程序崩溃,但有以下异常:
java.lang.RuntimeException: Unable to get provider androidx.core.content.FileProvider: java.lang.SecurityException: Provider must not be exported
我猜测即时应用程序不能使用FLAG_GRANT_READ_URI_PERMISSION标志,原因是它们不能使用WRITE_EXTERNAL_STORAGE权限。
我们还有其他方法可以在即时应用程序中共享图像吗?
即时应用程序不能导出ContentProvider
。这是一项安全限制,因此在此处崩溃的应用程序可以按预期运行。
您可以InstantApps.showInstallPrompt()
在启动Intent之前使用,以便让用户在执行此操作之前安装该应用程序。请确保您显示一条包含您的基本原理的消息,否则可能会使您的用户感到困惑。
还有其他使用即时应用程序共享图像的方法。但是这些取决于图像的来源。如果是外部内容提供商(即Camera应用程序的提供商),则您应该能够转发URI。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句