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

本文详细介绍了如何在Android系统中使用VCARD3.0标准来创建和读取联系人名片,提供了WriteExample和ReadExample两个代码示例,分别用于生成和解析vCard文件,实现联系人信息的存储和读取。示例基于android-vcard.jar库,对于Android开发者处理通讯录数据有一定参考价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

研究了几天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实现,带源码。有兴趣可以私信沟通交流学习。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值