将Android的contacts2.db导出成vcard联系人的方法

从Android的contacts2.db中导出vcard联系人方法,只简单说明下步骤

1:拿到contacts2.db文件

两种方法

(1):通过手机的RE管理工具将目录data/data/com.android.providers.contacts/databases/contacts2.db文件拷贝出来

(2):通过Recovery备份的文件data.img用unyaff程序解压缩后,可找到对应的data/data/com.android.providers.contacts/databases/contacts2.db文件,该方法适用于进不了系统的情况

2:使用java sqlite驱动包

下载地址:http://www.ch-werner.de/javasqlite/

3:使用android的vcard jar包

下载地址:http://code.google.com/p/android-vcard/

4:编写java代码进行数据的导出

(1):先读取数据库中contacts表,生成用户名和ID的对象

(2):遍历用户列表,根据用户ID读取raw_contacts和data表获取号码、地址等信息(两个表需要关联)

判断表中mimetype_id类型,一般mimetype_id值的定义:3: 地址 data1 5: 手机号码 data1 8: 名称 data1全名 data2名 data3姓 9:其他信息 data1地址 data4职位

(3):遍历用户列表,将用户信息导出到vcard文件

附上代码:

UserData

package org.rwl;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with PolarRwl.
 * User: polarrwl
 * Date: 13-5-23
 * Time: 下午3:46
 * To change this template use File | Settings | File Templates.
 */
public class UserData {
    private String userid;
    private String name;
    private String email;
    private String address;
    private String duty;
    private List<String> phones = new ArrayList<String>();

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public List<String> getPhones() {
        return phones;
    }

    public void setPhones(List<String> phones) {
        this.phones = phones;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getDuty() {
        return duty;
    }

    public void setDuty(String duty) {
        this.duty = duty;
    }

    public String toString() {
        String result = "";
        result += "ID:(" + userid;
        result += ") 姓名:(" + name;
        result += ") 邮件:(" + email;
        result += ") 号码:(" + phones;
        result += ") 地址:(" + address;
        result += ") 职位:(" + duty;
        result += ")";
        return result;
    }
}

TestSqlite

package org.rwl.sqlite;

import a_vcard.android.provider.Contacts;
import a_vcard.android.syncml.pim.vcard.ContactStruct;
import a_vcard.android.syncml.pim.vcard.VCardComposer;
import org.rwl.UserData;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with PolarRwl.
 * User: polarrwl
 * Date: 13-5-23
 * Time: 下午2:55
 * To change this template use File | Settings | File Templates.
 */
public class TestSqlite {
    public static void main(String[] args) {
        Map<String,UserData> repeat = new HashMap<String,UserData>();
        System.out.println("读取所有的联系人");
        List<UserData> theDataList = getAllContacts();
        System.out.println("处理联系人号码信息");
        for(UserData uData : theDataList) {
            analyseUserOtherData(uData);
        }
        System.out.println("打印联系人信息:");
        int i= 0;
        for(UserData uData : theDataList) {
            i++;
//            System.out.println(i + ":" + uData);
//            exportToVcard(uData);
            if(repeat.get(uData.getName()) != null) {
                System.out.println("重名:" + uData + "[" + repeat.get(uData.getName()) + "]");
            }
            repeat.put(uData.getName(), uData);

        }

    }

    /**
     * mimetype_id
     * 3: 地址 5: 手机号码 8: 名称 data1全名 data2名 data3姓 9: data1地址 data4职位 10:
     *
     * @param _udata
     */
    public static void analyseUserOtherData(UserData _udata) {

        Connection conn = null;
        try {
            conn = getConnection2();
            Statement stmt = conn.createStatement();
            String sql = "select * from raw_contacts t1, data t2 where t1._id = t2.raw_contact_id";
            sql += " and t1.contact_id = " + _udata.getUserid();
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()) {
                int mimetype_id = rs.getInt("mimetype_id");
                String data1 = rs.getString("data1");
                String data2 = rs.getString("data2");
                String data3 = rs.getString("data3");
                String data4 = rs.getString("data4");
                if(mimetype_id == 3) {
                    _udata.setAddress(data1);
                } else if(mimetype_id == 5) {
                    if(data1 != null && !_udata.getPhones().contains(data1)) {
                        _udata.getPhones().add(data1);
                    }
                } else if(mimetype_id == 9) {
                    _udata.setAddress(data1);
                    _udata.setDuty(data4);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        }
    }

    public static void exportToVcard(UserData _data) {
        OutputStreamWriter writer = null;
        try {
            writer = new OutputStreamWriter(new FileOutputStream("outputvcard/" + _data.getName() + ".vcf"), "UTF-8");
            VCardComposer composer = new VCardComposer();

            ContactStruct contact1 = new ContactStruct();
            contact1.name = _data.getName();
            contact1.company = "";
            contact1.notes = new ArrayList<String>();
            contact1.notes.add(_data.getAddress());

            for(int i=0; i<_data.getPhones().size(); i++) {
                if(_data.getPhones().get(i).length() == 11) {
                    contact1.addPhone(Contacts.Phones.TYPE_MOBILE, _data.getPhones().get(i), null, true);
                } else {
                    contact1.addPhone(Contacts.Phones.TYPE_OTHER, _data.getPhones().get(i), 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
            // ...
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        }

    }

    public static List<UserData> getAllContacts() {
        List<UserData> theResList = new ArrayList<UserData>();
        Connection conn = null;
        try {
            conn = getConnection2();
            Statement stmt = conn.createStatement();
            String sql = "select * from contacts";
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()) {
                int theId = rs.getInt("_id");
                String name = rs.getString("display_name");
                if(name == null || name.length() < 0) {
                    continue;
                }
                UserData theData = new UserData();
                theData.setUserid(""+theId);
                theData.setName(name);
                theResList.add(theData);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        }

        return theResList;
    }

    public static Connection getConnection2() {
        Connection conn = null;
        SQLite.Database db = null;
        try {
            Class.forName("SQLite.JDBCDriver").newInstance();
            conn = DriverManager.getConnection("jdbc:sqlite:database/contacts2.db");
//            java.lang.reflect.Method m =
//                    conn.getClass().getMethod("getSQLiteDatabase", null);
//            db = (SQLite.Database) m.invoke(conn, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }


}

你可能感兴趣的:(将Android的contacts2.db导出成vcard联系人的方法)