如何从包含联系人详细信息的对象生成.vcf文件,并且对象不在电话簿中



我想为一个对象生成一个.vcf文件,其中包含姓名、图像、电话号码、传真号码、电子邮件 ID、地址等联系信息。此对象不会添加到电话的地址簿中,而是存储在我的应用程序中。

生成.vcf文件后,我可以像这样发送此电子名片

Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("**generated.vcf**"), "text/x-vcard");
startActivity(i);

但是我不知道如何获取这个生成的.vcf文件?

生成.vcf文件实际上很容易。 看看VCF格式 - 它是一个简单的文本文件。 您需要做的就是创建文本文件并使用VCF字段将信息写入其中。 你最终会得到这样的东西:

Person p = getPerson();
File vcfFile = new File(this.getExternalFilesDir(null), "generated.vcf");
FileWriter fw = new FileWriter(vcfFile);
fw.write("BEGIN:VCARDrn");
fw.write("VERSION:3.0rn");
fw.write("N:" + p.getSurname() + ";" + p.getFirstName() + "rn");
fw.write("FN:" + p.getFirstName() + " " + p.getSurname() + "rn");
fw.write("ORG:" + p.getCompanyName() + "rn");
fw.write("TITLE:" + p.getTitle() + "rn");
fw.write("TEL;TYPE=WORK,VOICE:" + p.getWorkPhone() + "rn");
fw.write("TEL;TYPE=HOME,VOICE:" + p.getHomePhone() + "rn");
fw.write("ADR;TYPE=WORK:;;" + p.getStreet() + ";" + p.getCity() + ";" + p.getState() + ";" + p.getPostcode() + ";" + p.getCountry() + "rn");
fw.write("EMAIL;TYPE=PREF,INTERNET:" + p.getEmailAddress() + "rn");
fw.write("END:VCARDrn");
fw.close();
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(vcfFile), "text/x-vcard");
startActivity(i);

(请注意,此代码将放置在活动中。 如果它不在活动中,则将getExternalFilesDir前面的this替换为 Context 的实例

您可以根据需要拥有更多更少的字段。 如果字段值中有 ,; 个字符,则需要使用 对它们进行转义;要将换行符放入值中,请将\n写入文件(即文件本身必须包含n,第二个斜杠用于转义换行符中的斜杠)。

这段代码非常粗糙,但它应该让你入门。 再次,看看VCF的格式,然后从那里开始。

更新:感谢@Michael指出我原始答案中的错误。

您可能

有兴趣使用 vCard 库,而不是手动创建 vCard 字符串。这样,您就不必担心所有格式细节,例如要转义的字符。 EZ-vcard就是这样一个图书馆。

使用 ez-vcard,@AleksG代码示例中的 vCard 将生成如下:

Person p = getPerson();
File vcfFile = new File(this.getExternalFilesDir(null), "generated.vcf");
VCard vcard = new VCard();
vcard.setVersion(VCardVersion.V3_0);
StructuredNameType n = new StructuredNameType();
n.setFamily(p.getSurname());
n.setGiven(p.getFirstName());
vcard.setStructuredName(n);
vcard.setFormattedName(new FormattedNameType(p.getFirstName() + " " + p.getSurname()));
OrganizationType org = new OrganizationType();
org.addValue(p.getCompanyName());
vcard.setOrganization(org);
vcard.addTitle(new TitleType(p.getTitle()));
TelephoneType tel = new TelephoneType(p.getWorkPhone());
tel.addType(TelephoneTypeParameter.WORK));
tel.addType(TelephoneTypeParameter.VOICE));
vcard.addTelephoneNumber(tel);
tel = new TelephoneType(p.getHomePhone());
tel.addType(TelephoneTypeParameter.HOME));
tel.addType(TelephoneTypeParameter.VOICE));
vcard.addTelephoneNumber(tel);
AddressType adr = new AddressType();
adr.setStreetAddress(p.getStreet());
adr.setLocality(p.getCity());
adr.setRegion(p.getState());
adr.setPostalCode(p.getPostcode());
adr.setCountry(p.getCountry());
adr.addType(AddressTypeParameter.WORK);
vcard.addAddress(adr);
EmailType email = new EmailType(p.getEmailAddress());
email.addType(EmailTypeParameter.PREF);
email.addType(EmailTypeParameter.INTERNET);
vcard.addEmail(email);
vcard.write(vcfFile);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(vcfFile), "text/x-vcard");
startActivity(i);

相关内容

最新更新