How to generate a .vcf file from an object which contains contact detail and object is not in the phone book

淺唱寂寞╮ 提交于 2019-12-03 13:00:49

问题


I want to generate a .vcf file for an object which contains contact information like name, image, phone number, fax number, email id, address etc. This object is not added in the address book of the phone but it is stored in my application.

Once my .vcf file is generated I can send this vcard like this

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

But I am not getting how to get this generated .vcf file?


回答1:


It's actually quite easy to generate a .vcf file. Have a look at VCF format - it's a simple text file. All you need to do is create text file and write info into it using the VCF fields. You'll end up with something like this:

Person p = getPerson();

File vcfFile = new File(this.getExternalFilesDir(null), "generated.vcf");
FileWriter fw = new FileWriter(vcfFile);
fw.write("BEGIN:VCARD\r\n");
fw.write("VERSION:3.0\r\n");
fw.write("N:" + p.getSurname() + ";" + p.getFirstName() + "\r\n");
fw.write("FN:" + p.getFirstName() + " " + p.getSurname() + "\r\n");
fw.write("ORG:" + p.getCompanyName() + "\r\n");
fw.write("TITLE:" + p.getTitle() + "\r\n");
fw.write("TEL;TYPE=WORK,VOICE:" + p.getWorkPhone() + "\r\n");
fw.write("TEL;TYPE=HOME,VOICE:" + p.getHomePhone() + "\r\n");
fw.write("ADR;TYPE=WORK:;;" + p.getStreet() + ";" + p.getCity() + ";" + p.getState() + ";" + p.getPostcode() + ";" + p.getCountry() + "\r\n");
fw.write("EMAIL;TYPE=PREF,INTERNET:" + p.getEmailAddress() + "\r\n");
fw.write("END:VCARD\r\n");
fw.close();

Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(vcfFile), "text/x-vcard");
startActivity(i);

(Note that this code is to be placed inside an activity. If it's not in an activity, then replace this in front of getExternalFilesDir with an instance of Context.)

You can have more of fewer fields, as needed. If you have ,, ; or \ characters in field values, they need to be escaped with \; to put a newline character into a value, write \\n into the file (i.e. the file itself must contain \n, the second slash is for escaping the slash in the newline).

This code is quite crude, but it should get you started. Again, have a look at the format of VCF and go from there.

Update: Thanks to @Michael for pointing out mistakes in my original answers.




回答2:


You might be interested in using a vCard library instead of creating the vCard string by hand. That way, you don't have to worry about all the formatting details, like which characters to escape. ez-vcard is one such library.

Using ez-vcard, the vCard in @AleksG's code sample would be generated like so:

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);


来源:https://stackoverflow.com/questions/13702357/how-to-generate-a-vcf-file-from-an-object-which-contains-contact-detail-and-obj

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