Reputation: 3552
Is there any way to parse contact VCF file programmatically ?
I able to create .vcf from contacts but unable to parse in my code.
Upvotes: 0
Views: 3883
Reputation: 2184
Just download ezvcard library and add it to project and use following function to read vcf file. I have checked it and working fine for me.
public void readVCF(){
try{
File file = new File(Environment.getExternalStorageDirectory()+"/temp.vcf");
List<VCard> vcards = Ezvcard.parse(file).all();
for (VCard vcard : vcards){
System.out.println("Name: " + vcard.getFormattedName().getValue());
System.out.println("Telephone numbers:");
for (Telephone tel : vcard.getTelephoneNumbers()){
System.out.println(tel.getTypes() + ": " + tel.getText());
}
}
}catch(Exception e){e.printStackTrace();}}
Upvotes: 1
Reputation: 3552
With the help of ez-vCard, a external library, I am able to parse vcf file. But still not able to parse it using any Android built-in API.
String backup_file_path = Environment.getExternalStorageDirectory().getPath() + "/contact_backup.vcf";
File backup_file = new File(backup_file_path);
List<VCard> vcard;
try {
vcard = Ezvcard.parse(backup_file).all();
for (VCard vCard2 : vcard) {
//System.out.println("Name: "+ vCard2.getFormattedName().getValue());
//System.out.println("Email: "+ vCard2.getEmails().get(0).getValue());
//System.out.println("Phone No: "+ vCard2.getTelephoneNumbers().get(0));
//Get list of phone numbers
List<Telephone> telePhoneNumbers = vCard2.getTelephoneNumbers();
for (int i = 0; i < telePhoneNumbers.size(); i++) {
System.out.println("Phone No: "+ telePhoneNumbers.get(i).getText());
}
}
} catch (IOException e1) {
e1.printStackTrace();
}
Upvotes: 0