我想通过发送意图在用户的联系人中保存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();
}
答案 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();
}
}