联系人备份--vcf

是什么

vcf是通讯录导出的一种格式

怎么样

百度是这样说的

VCF格式通讯录格式现在用途广泛,一般诺基亚、摩托罗拉手机导出通讯录的格式即为VCF。可以把VCF格式保存到电脑上以备不时之需。而且现在很多网上通讯录的导入格式也是VCF,现在软件就加入了导入联系人的功能,可以把手机通讯录导入飞信的通讯录里,这样不管你手机在不在,只要有一台电脑就可以对自己的通讯录里的人了如指掌了。
需要强调的的是,现在一般导入VCF基本都有乱码,可以用工具VCFEncoding工具重新导入一下,这样就可以解决乱码了。还可以在网上找一些网络通讯录,里面基本都会有用记事本改VCF的功能,可是,一个一个改比较繁琐,建议用第一种方法,省时省力。
上面说导入VCF格式通讯录。下面方法是比较简便的,即向电脑导入CSV通讯录格式。因为VCF格式是一个联系人一个VCF文件,导入网络通讯录时得一个一个导入,而CSV则是所有通讯录都集成在一个文件里,这样导入的话就比较方便。先下载转换工具超级强档,之后安装,安装后运行,点文件,其中有一栏为导入,导入VCF的文件夹就可以,之后在点文件,有一栏为导出,导出CSV文件就可以了,例如将通讯录里的人导入飞信就可以用以上步骤,最后将CSV导入飞信就可以了。CSV可以用Excel编辑,自己看情况可以另外编辑。
也可以直接用WPS表格Excel打开VCF格式文件,打开后是表格。也可以对表格进行修改。

怎么用

首先导入vcrad-android包 3.0版本的
点击可下载
1,生成vcf

    String fileName = l + ".vcf";
                mFile = new File(absolutePath, fileName);
                try {
                    writer = new OutputStreamWriter(new FileOutputStream(mFile), "UTF-8");
                    //create a contact
                    VCardComposer composer = new VCardComposer();
                    JSONArray ContactsArray = new JSONArray(str);
                    String vcardString;
                    for (int i = 0; i < ContactsArray.length(); i++) {
                        ContactStruct contactStruct = new ContactStruct();
                        JSONObject ContactsObject = (JSONObject) ContactsArray.get(i);
                        String localname = ContactsObject.getString("name");
                        String localphone = ContactsObject.getString("phone");
                        Log.i("Contact", "---localname---" + localname + "---localphone---" + localphone);
                        contactStruct.name = localname;
                        contactStruct.addPhone(Contacts.Phones.TYPE_MOBILE, localphone, null, true);
                        vcardString = composer.createVCard(contactStruct, VCardComposer.VERSION_VCARD30_INT);
                        writer.write(vcardString);
                        Log.i("Contact", "正在写入-----");
                    }
                    Log.i("Contact", "写入完成------");
                    writer.close();

这样,在filename的文件目录下就写入了一个vcf文件。
2,解析vcf

  //
            mContactsFile = new File(backUpfile.getAbsolutePath(), name);
            long length = mContactsFile.length();
            Log.i("Contact", "length:--" + length);
            VCardParser parser = new VCardParser();
            VDataBuilder builder = new VDataBuilder();
            try {
                //read whole file to string
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        new FileInputStream(mContactsFile), "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: " + mContactsFile);
                }
                //get all parsed contacts
                List<VNode> pimContacts = builder.vNodeList;
                mLocalSize = pimContacts.size();

这样解析出来的是一个集合,集合里面有所有的联系人数据
那么,如何获取手机联系人的姓名和手机号呢
获取名称简单

                 for (VNode contact : pimContacts) {
                                    ArrayList<PropertyNode> props = contact.propList;
                                    // contact name - FN property
                                    String DisplayName = null;
                                    for (PropertyNode prop : props) {
                                        if ("FN".equals(prop.propName)) {
                                            DisplayName = prop.propValue;
                                            String propValue = prop.propValue;

                                            break;
                                        }

                                    }

获取手机号,有一点点麻烦,找了好久才找到

  ContactStruct contactStruct = ContactStruct.constructContactFromVNode(contact, 1);
                //获取备份文件中的联系人电话信息
                List<ContactStruct.PhoneData> phoneDataList = contactStruct.phoneList;
                if (phoneDataList != null) {
                    for (ContactStruct.PhoneData phoneData : phoneDataList) {
                        String data = phoneData.data;
                        int type = phoneData.type;
                        LogUtils.i(data + "-----type:---" + type);
                        contactInfo.setPhone(data);
                    }

姓名是通过VNode 对象去取,手机号是通过contactStruct,所以要通过ContactStruct.constructContactFromVNode(contact, 1)方法把,VNode转换成ContactStruct
完,后续补充

猜你喜欢

转载自blog.csdn.net/cdhahaha/article/details/77530309