β

VCARD3.0通讯录名片在Android系统的解析读写

吴宇生的博客 30 阅读
研究了几天VCARD3.0,发现网上资料太少,特别是在Android平台的。特此记录。

输出名片类范例:

public class WriteExample {

    public static void main(String[] args) throws Exception {

        OutputStreamWriter writer = new OutputStreamWriter(

                new FileOutputStream("example.vcard"), "UTF-8");

        VCardComposer composer = new VCardComposer();

        //create a contact

        ContactStruct contact1 = new ContactStruct();

        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 between contacts

        // repeat for other contacts

        // ...

        writer.close();

    }

}

输入名片类范例:

public class ReadExample {

    //run the WriteExample first or provide your own "example.vcard"

    public static void main(String[] args) throws Exception {

        VCardParser parser = new VCardParser();

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

        String file = "example.vcard";

        //read whole file to string

        BufferedReader reader = new BufferedReader(new InputStreamReader(

                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 new VCardException("Could not parse vCard file: " + file);

        }

        //get all parsed contacts

        List pimContacts = builder.vNodeList;

        //do something for all the contacts

        for (VNode contact : pimContacts) {

            ArrayList props = contact.propList;

            //contact name - FN property

            String name = null;

            for (PropertyNode prop : props) {

            System.out.println(prop.propName + ":" +prop.propValue);

            }

            //similarly for other properties (N, ORG, TEL, etc)

            //...

        }

    System.out.println("Read Successsssss!");

    }

}

以上范例基于android-vcard.jar实现,带源码。有兴趣可以私信沟通交流学习。

作者:吴宇生的博客
原文地址:VCARD3.0通讯录名片在Android系统的解析读写, 感谢原作者分享。

发表评论