将vCard附加到电子邮件意图

时间:2017-07-17 19:14:51

标签: android android-intent email-attachments

我正在尝试将vCard(* .vcf)附加到电子邮件意图中:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Shared Contact via the Foo App");

// vcfFile is a *.vcf file on local storage
Uri vCardUri = FileProvider.getUriForFile(this, "com.foo.fileprovider", vcfFile);
emailIntent.setData(vCardUri);
startActivity(emailIntent);

然而,应用程序死亡时出现以下异常:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SENDTO dat=content://com.foo.fileprovider/external_files/Android/data/com.foo/files/generated.vcf (has extras) }

我也尝试过显式设置这样的类型:

emailIntent.setDataAndType(vCardUri, "text/x-vcard");

然而,它也爆发了:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SENDTO dat=content://com.foo.fileprovider/external_files/Android/data/com.foo/files/generated.vcf typ=text/x-vcard (has extras) }

有没有办法将vCard附加到电子邮件意图?

1 个答案:

答案 0 :(得分:0)

仅记录共享文件以与ACTION_SEND一起使用。将Uri放入EXTRA_STREAM。 (可能也可以与ACTION_SENDTO一起使用某些电子邮件应用。)

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Shared Contact via the Foo App");

// vcfFile is a *.vcf file on local storage
Uri vCardUri = FileProvider.getUriForFile(this, "com.foo.fileprovider", vcfFile);
emailIntent.putExtra(Intent.EXTRA_STREAM, vCardUri);
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent.setType("text/x-vcard");
startActivity(Intent.createChooser(emailIntent, null));