VCARD3.0通讯录名片在Android系统…

来源:互联网 发布:剪刀手软件 编辑:程序博客网 时间:2024/05/22 05:30
研究了几天VCARD3.0,发现网上资料太少,特别是在Android平台的。特此记录。

输出名片类范例:
public class WriteExample {

    public static voidmain(String[] args) throws Exception {

       OutputStreamWriter writer = newOutputStreamWriter(
              newFileOutputStream("example.vcard"), "UTF-8");

       VCardComposer composer = newVCardComposer();

       //create a contact
       ContactStruct contact1 = newContactStruct();
       contact1.name = "WuYusheng";
       contact1.company = "The Company";
       contact1.addPhone(Contacts.Phones.TYPE_MOBILE,"+861888888888", null, true);

       //create vCard representation
       String vcardString =composer.createVCard(contact1,VCardComposer.VERSION_VCARD30_INT);

       //write vCard to the output stream
       writer.write(vcardString);
       writer.write("\n"); //add empty lines betweencontacts

       // repeat for other contacts
       // ...

       writer.close();
    }
}

输入名片类范例:
public class ReadExample {

    //run the WriteExamplefirst or provide your own "example.vcard"

    public static voidmain(String[] args) throws Exception {

       VCardParser parser = new VCardParser();
       VDataBuilder builder = newVDataBuilder();//VDataBuilder("UTF-8", "UTF-8", false);
 

       String file = "example.vcard";

       //read whole file to string
       BufferedReader reader = new BufferedReader(newInputStreamReader(
              new FileInputStream(file),"UTF-8"));
       
       String vcardString = "";
       String line;
       while ((line = reader.readLine()) != null){
          vcardString += line + "\n";
       }
       reader.close();

       //parse the string
       boolean parsed = parser.parse(vcardString,"UTF-8", builder);
       if (!parsed) {
           throw newVCardException("Could not parse vCard file: " + file);
       }

       //get all parsed contacts
       List pimContacts = builder.vNodeList;

       //do something for all the contacts
       for (VNode contact : pimContacts) {
           ArrayListprops = contact.propList;

           //contactname - FN property
           Stringname = null;
           for(PropertyNode prop : props) {
             
          System.out.println(prop.propName + ":" +prop.propValue);
           }

          //similarly for other properties (N, ORG, TEL, etc)
          //...
       }
    System.out.println("ReadSuccesssssss!");
    }
}
以上范例基于android-vcard.jar实现,带源码。有兴趣可以私信沟通交流学习。
0 0
原创粉丝点击