android - 您如何通过即时应用程序中的 Intent 共享图像?

标签 android android-instant-apps dynamic-feature

我们正在构建一个测验即时应用程序,用户可以在其中完成测验,然后分享他们的结果。我们共享一些带链接的文本,以及显示用户测验结果的图像。当我们在安装的应用程序中执行此流程时没有问题,但是在免安装应用程序中,图像无法共享。

以下是我们如何生成 Intent :

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

这是我们基础应用程序 list 中的提供者:

<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 之前,以便让用户在执行此操作之前安装应用程序。请确保您显示的消息包含您的理由,否则您可能会使您的用户感到困惑。

还有其他方法可以使用免安装应用分享图片。但这些取决于图像的来源。如果是外部内容提供商(即相机应用程序),您应该能够转发 URI。

关于android - 您如何通过即时应用程序中的 Intent 共享图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57161562/

相关文章:

Android Production Instant App 未在 Google Play 商店中显示 "Try Now"按钮

android - 拆分安装 API 不可用。安装动态功能 Android Studio 时出现错误代码 - 5

android - 为什么android studio要组装我们的动态功能模块测试apk?

java - 具有不同布局的 Android RecyclerView 不起作用

java - Android 中 localTime 和 localDate 的替代类是什么?

java - Xmlparser.getText() 给出 null

android - 如何使用Zxing Library获取扫描图片和二维码

android - 如何将已安装应用程序中的组件公开给即时应用程序可见?

android - IllegalStateException : Expected configuration ':module:debugFeatureCompileClasspath' to contain exactly one file, 但是,它包含 2 个文件