通过意图保存VCard联系人

时间:2015-02-16 15:55:16

标签: android android-intent vcard

我想通过发送意图在用户的联系人中保存VCard格式的联系人数据。有没有办法做到这一点?

注意:我不想将VCard个数据保存在.vcf文件中,然后将其uri提供给intent个下面的代码。

String scanned = "..." // contact in VCard format
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
File vcfFile = new File(getCacheDir(), "tmp.vcf");
try {
    FileOutputStream fos = new FileOutputStream(vcfFile);
    OutputStreamWriter osw = new OutputStreamWriter(fos);
    osw.write(scanned);
    osw.close();
    fos.close();
    i.setDataAndType(Uri.fromFile(vcfFile), "text/vcard");
    startActivity(i);
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

1 个答案:

答案 0 :(得分:1)

如果联系人确实在联系人数据库中,您可以查看android联系人应用程序的代码,以查看如何从名称中共享vcard(从here中找到) " QuickContactActivity.java"):

private void shareContact() {
    final String lookupKey = mContactData.getLookupKey();
    final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
    final Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType(Contacts.CONTENT_VCARD_TYPE);
    intent.putExtra(Intent.EXTRA_STREAM, shareUri);

    // Launch chooser to share contact via
    final CharSequence chooseTitle = getText(R.string.share_via);
    final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);

    try {
        mHasIntentLaunched = true;
        ImplicitIntentsUtil.startActivityOutsideApp(this, chooseIntent);
    } catch (final ActivityNotFoundException ex) {
        Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
    }
}