12

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?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Shaista Naaz
  • 8,281
  • 9
  • 37
  • 50
  • elaborate your problem. what's object.? have you fetched contact list.? have you created vcard.? – Sahil Mahajan Mj Dec 04 '12 at 12:15
  • @SahilMahajanMj I think the OP means that he has _an_ object with some person's information in it and he wants to create a vcf out of it; this info has nothing to do with the phone's contacts. – Aleks G Dec 04 '12 at 12:16

2 Answers2

27

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.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • +1, good explanation. will it send .vcf file as an mms.? is there any way to send vcard string as sms to other phone, because some phones(other than android) dont support mms. – Sahil Mahajan Mj Dec 04 '12 at 12:19
  • @SahilMahajanMj This question isn't about how to send a VCF but rather about how to generate one. Have a look at [this question](http://stackoverflow.com/questions/10420312/android-how-to-attach-file-to-sms) regarding sending it by SMS. – Aleks G Dec 04 '12 at 12:22
  • I am not talking about this question. I was just asking if you know how to send it as sms. – Sahil Mahajan Mj Dec 04 '12 at 12:24
  • @SahilMahajanMj The answer is in the question I linked to from my comment. – Aleks G Dec 04 '12 at 12:26
  • @AleksG Colon characters do not need to be escaped in field values. Also, comma characters should be escaped. So the characters you should escape are: `;`, `\ `, `,`, and newlines. Also, the correct newline sequence to use for separating fields is "\r\n" not "\n". – Michael Dec 05 '12 at 13:41
  • @Michael Thanks. I'm updating the answer accordingly. – Aleks G Dec 05 '12 at 13:52
  • @AleksG Np. I could see how you might be confused about thinking that colons must be escaped. There are a number of popular mail clients which incorrectly escape colons, such as GMail and the iPhone. – Michael Dec 05 '12 at 20:27
  • what if I want to share this contact information with other application, say whatsapp ?? What changes i have to make in above? – Uniruddh Jul 03 '14 at 14:33
  • @I-droid I'm not sure what this have to do with this question. Please post a separate question for this - but search to make sure that you don't post duplicates. – Aleks G Jul 03 '14 at 14:45
  • but i am unable to export multiple Contact using for loop using given code – Maklee Lee Jan 04 '18 at 12:22
  • how to save ,Familyinfromation,birthday,Assistant,website,notes an all can u please tell @AleksG – Mhanaz Syed Jan 06 '18 at 03:47
  • https://stackoverflow.com/questions/48123920/how-to-export-all-contact-information-as-vcf-file-in-android please check this @Akshay – Mhanaz Syed Jan 06 '18 at 04:39
6

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);
Michael
  • 34,873
  • 17
  • 75
  • 109
  • https://stackoverflow.com/questions/48123920/how-to-export-all-contact-information-as-vcf-file-in-android @Michael – Mhanaz Syed Jan 06 '18 at 04:40