How to parse contact .vcf file programmatically?

2019-09-04 04:59发布

Is there any way to parse contact VCF file programmatically ?

I able to create .vcf from contacts but unable to parse in my code.

标签: android
2条回答
贪生不怕死
2楼-- · 2019-09-04 05:27

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();}}
查看更多
Lonely孤独者°
3楼-- · 2019-09-04 05:36

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();
    }
查看更多
登录 后发表回答