我正在尝试与电子邮件客户端和Google云端硬盘共享文件。现在,在以下代码中,只有Google驱动器正在打开,而电子邮件客户端根本没有打开。如果需要,我可以提供以下代码的等效Java代码
val photoURI: Uri = FileProvider.getUriForFile(this, "com.emerson.oversight.com.emerson.oversight.provider",
File(this.cacheDir.path + "/SensorReport.pdf"))
val emailIntent = Intent(Intent.ACTION_SENDTO)
emailIntent.data = Uri.parse("mailto:")
emailIntent.putExtra(Intent.EXTRA_STREAM, photoURI)
emailIntent.putExtra(Intent.EXTRA_EMAIL, "asd@dsa.dsa")
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "dsadsada")
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val driveIntent = Intent()
driveIntent.`package`= "com.google.android.apps.docs"
driveIntent.action = Intent.ACTION_VIEW
val fileID = File(this.cacheDir.path + "/SensorReport.pdf")
val url = "https://docs.google.com/file/d/" + fileID
driveIntent.data = Uri.parse(url)
val openInChooser = Intent.createChooser(driveIntent, getString(R.string.share_using))
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayListOf(emailIntent))
startActivity(openInChooser)
请帮忙
答案 0 :(得分:1)
你几乎就在那里,谜题中唯一缺少的部分是getPackageManager().queryIntentActivities
方法,它将返回所有可以处理你的电子邮件意图的活动。返回ResolveInfo后,您可以为选择器中显示的每个电子邮件选项构建一个意图。然后,您可以将这些意图的数组作为Intent.EXTRA_INITIAL_INTENTS
传递。如果您愿意,甚至可以排除某些包裹。所以代码的最后部分看起来像这样:
val openInChooser = Intent.createChooser(driveIntent, getString(R.string.share_using))
val emailOptionIntents = mutableListOf<Intent>()
val resInfo = getPackageManager().queryIntentActivities(emailIntent, 0)
if (!resInfo.isEmpty()) {
for (resolveInfo in resInfo) {
val emailOptionIntent = Intent(Intent.ACTION_SENDTO)
emailOptionIntent.data = Uri.parse("mailto:")
emailOptionIntent.putExtra(Intent.EXTRA_STREAM, photoURI)
emailOptionIntent.putExtra(Intent.EXTRA_EMAIL, "asd@dsa.dsa")
emailOptionIntent.putExtra(Intent.EXTRA_SUBJECT, "dsadsada")
emailOptionIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
emailOptionIntent.`package` = resolveInfo.activityInfo.packageName
emailOptionIntents.add(emailOptionIntent)
}
}
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, emailOptionIntents.toTypedArray())
startActivity(openInChooser)