【问题标题】:How to parse contact .vcf file programmatically?如何以编程方式解析联系人 .vcf 文件?
【发布时间】:2014-11-06 10:19:16
【问题描述】:

有没有办法以编程方式解析联系人 VCF 文件?

我能够从联系人创建 .vcf,但无法解析我的代码。

【问题讨论】:

标签: android


【解决方案1】:

只需下载 ezvcard 库并将其添加到项目中,然后使用以下函数读取 vcf 文件。我已经检查过了,对我来说工作正常。

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

【讨论】:

    【解决方案2】:

    借助 ez-vCard,一个外部库,我能够解析 vcf 文件。但仍然无法使用任何 Android 内置 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();
        }
    

    【讨论】:

      猜你喜欢
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-13
      • 2020-11-10
      • 2016-12-26
      相关资源
      最近更新 更多