问题
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