Saving VCard contact via intent

守給你的承諾、 提交于 2019-12-13 18:25:57

问题


I want to save a contact data which is in VCard format in user's contacts via sending intent. Is there any way to do it?

NOTE: I don't want to save VCard data in a .vcf file and then give it's uri to ‍‍intent like the code below.

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:


If the contact is indeed in the contacts database, you could look at the android contacts app's code, to see how to share a vcard out of it (found from here, inside a file called "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();
    }
}


来源:https://stackoverflow.com/questions/28545346/saving-vcard-contact-via-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!