代码之家  ›  专栏  ›  技术社区  ›  Perry Hoekstra

将vCard附加到电子邮件意图

  •  0
  • Perry Hoekstra  · 技术社区  · 8 年前

    我正试图将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 回复  |  直到 8 年前
        1
  •  0
  •   cketti    8 年前

    共享文件仅用于记录 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));