(转)Android 读取联系人(详细)

摘自:http://blog.csdn.net/ydpl2007/article/details/8118109
 
  1. import java.io.InputStream;  
  2. import org.json.JSONArray;  
  3. import org.json.JSONException;  
  4. import org.json.JSONObject;  
  5. import android.content.ContentUris;  
  6. import android.content.Context;  
  7. import android.database.Cursor;  
  8. import android.net.Uri;  
  9. import android.provider.ContactsContract;  
  10. import android.util.Log;  
  11.   
  12. public class CopyOfContactCollector {  
  13.   
  14.     private static final String TAG = CopyOfContactCollector.class.getSimpleName();  
  15.       
  16.     private static final String KEY_BIRTH = "birthday";  
  17.     private static final String KEY_ADDR = "address";  
  18.     private static final String KEY_NICKNAME = "nickname";  
  19.     private static final String KEY_ORG = "org";  
  20.     private static final String KEY_IM = "IM";  
  21.     private static final String KEY_NOTE = "note";  
  22.     private static final String KEY_EMAIL = "email";  
  23.     private static final String KEY_PHONE = "phone";  
  24.     private static final String KEY_WEBSITE = "website";  
  25.     private static final String KEY_PHOTO = "photo";  
  26.       
  27.     private Context context;  
  28.   
  29.     public CopyOfContactCollector(Context context) {  
  30.         this.context = context;  
  31.     }  
  32.       
  33.     public void getContacts() {  
  34.   
  35.         Cursor cursor = null;  
  36.         try {  
  37.             cursor = context.getContentResolver().query(  
  38.                     ContactsContract.Contacts.CONTENT_URI,   
  39.                     null,  
  40.                     null,   
  41.                     null,   
  42.                     null);  
  43.               
  44.             JSONArray contactList = new JSONArray();  
  45.             while (cursor.moveToNext()) {  
  46.                 String contactId = cursor.getString(cursor  
  47.                         .getColumnIndex(ContactsContract.Contacts._ID));  
  48.                 int hasPhone = cursor.getInt(cursor  
  49.                         .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));  
  50.                 String contactName = cursor.getString(cursor  
  51.                         .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));  
  52.                 long photoId = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));  
  53.                   
  54.                 JSONObject item = new JSONObject();  
  55.                 item.put("id", contactId);  
  56.                 item.put("name", contactName);  
  57.                   
  58.                 // phone  
  59.                 if (hasPhone == 1) {  
  60.                     this.getPhone(contactId, item);  
  61.                 }  
  62.                   
  63.                 // photo  
  64.                 this.getPhoto(contactId, photoId, item);  
  65.                   
  66.                 // email  
  67.                 this.getEmail(contactId, item);  
  68.                  
  69.                 // address  
  70.                 this.getAddress(contactId, item);  
  71.                   
  72.                 // birthdat  
  73.                 this.getBirthday(contactId, item);  
  74.                   
  75.                 // instant message  
  76.                 this.getIM(contactId, item);  
  77.                   
  78.                 // nickname  
  79.                 this.getNickname(contactId, item);  
  80.                   
  81.                 // note  
  82.                 this.getNote(contactId, item);  
  83.                   
  84.                 // org  
  85.                 this.getOrg(contactId, item);  
  86.                   
  87.                 // website  
  88.                 this.getWebsite(contactId, item);  
  89.                   
  90.                 contactList.put(item);  
  91.             }  
  92.               
  93.             JSONObject data = new JSONObject();  
  94.             data.put("CONTACTS", contactList);  
  95.             data.put("TIMESTAMP", System.currentTimeMillis());  
  96.               
  97.             System.out.println(data.toString());  
  98.         } catch (Exception e) {  
  99.             e.printStackTrace();  
  100.         } finally {  
  101.             if (cursor != null) {  
  102.                 cursor.close();  
  103.             }  
  104.         }  
  105.     }  
  106.       
  107.     private void getPhone (String contactId, JSONObject data) throws JSONException {  
  108.         Cursor pCur = null;  
  109.         try {  
  110.             pCur = context.getContentResolver().query(  
  111.                     ContactsContract.CommonDataKinds.Phone.CONTENT_URI,  
  112.                     null,  
  113.                     ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",  
  114.                     new String[]{contactId + ""},   
  115.                     null);  
  116.               
  117.             JSONArray phoneList = new JSONArray();  
  118.             while (pCur.moveToNext()) {  
  119.                  int type = pCur.getInt(pCur.getColumnIndex(  
  120.                          ContactsContract.CommonDataKinds.Phone.TYPE));  
  121.                  String phoneType = ContactsContract.CommonDataKinds.Phone.getTypeLabel(  
  122.                          context.getResources(), type, "").toString();  
  123.                  String phoneNumber = pCur.getString(pCur.getColumnIndex(  
  124.                          ContactsContract.CommonDataKinds.Phone.NUMBER));  
  125.                
  126.                  JSONObject item = new JSONObject();  
  127.                  item.put("phone", phoneNumber);  
  128.                  item.put("type", phoneType);  
  129.                    
  130.                  phoneList.put(item);  
  131.             }  
  132.               
  133.             data.put(KEY_PHONE, phoneList);  
  134.         } finally {  
  135.             if (pCur != null) {  
  136.                 pCur.close();  
  137.             }  
  138.         }  
  139.     }  
  140.       
  141.     private void getEmail (String contactId, JSONObject data) throws JSONException {  
  142.         Cursor emailCur = null;  
  143.         try {  
  144.             emailCur = context.getContentResolver().query(  
  145.                     ContactsContract.CommonDataKinds.Email.CONTENT_URI,  
  146.                     null,  
  147.                     ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",  
  148.                     new String[]{contactId},   
  149.                     null);  
  150.               
  151.             JSONArray emailList = new JSONArray();  
  152.             while (emailCur.moveToNext()) {  
  153.                 String email = emailCur.getString(emailCur.getColumnIndex(  
  154.                             ContactsContract.CommonDataKinds.Email.DATA));  
  155.                 int type = emailCur.getInt(emailCur.getColumnIndex(  
  156.                             ContactsContract.CommonDataKinds.Email.TYPE));  
  157.                 String emailType = ContactsContract.CommonDataKinds.Email.getTypeLabel(  
  158.                             context.getResources(), type, "").toString();  
  159.                   
  160.                 JSONObject item = new JSONObject();  
  161.                 item.put("email", email);  
  162.                 item.put("type", emailType);  
  163.                 
  164.                 emailList.put(item);  
  165.             }  
  166.               
  167.             data.put(KEY_EMAIL, emailList);  
  168.         } finally {  
  169.             if (emailCur != null) {  
  170.                 emailCur.close();  
  171.             }  
  172.         }  
  173.     }  
  174.       
  175.     private void getNote (String contactId, JSONObject data) throws JSONException {  
  176.         Cursor noteCur = null;  
  177.         try {  
  178.             String noteWhere =   
  179.                 ContactsContract.Data.CONTACT_ID + " = ? AND " +   
  180.                 ContactsContract.Data.MIMETYPE + " = ?";  
  181.               
  182.             String[] noteWhereParams = new String[]{  
  183.                         contactId,  
  184.                         ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};  
  185.               
  186.             noteCur = context.getContentResolver().query(  
  187.                         ContactsContract.Data.CONTENT_URI,   
  188.                         null,   
  189.                         noteWhere,   
  190.                         noteWhereParams,   
  191.                         null);  
  192.             if (noteCur.moveToFirst()) {  
  193.                 String note = noteCur.getString(noteCur.getColumnIndex(  
  194.                             ContactsContract.CommonDataKinds.Note.NOTE));  
  195.                 data.put(KEY_NOTE, note);  
  196.             }  
  197.         } finally {  
  198.             if (noteCur != null) {  
  199.                 noteCur.close();  
  200.             }  
  201.         }  
  202.     }  
  203.       
  204.     private void getWebsite (String contactId, JSONObject data) throws JSONException {  
  205.         Cursor websiteCur = null;  
  206.         try {  
  207.             String where =   
  208.                 ContactsContract.Data.CONTACT_ID + " = ? AND " +   
  209.                 ContactsContract.Data.MIMETYPE + " = ?";  
  210.               
  211.             String[] whereParams = new String[]{  
  212.                         contactId,  
  213.                         ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE};  
  214.               
  215.             websiteCur = context.getContentResolver().query(  
  216.                         ContactsContract.Data.CONTENT_URI,   
  217.                         null,   
  218.                         where,   
  219.                         whereParams,   
  220.                         null);  
  221.             if (websiteCur.moveToFirst()) {  
  222.                 String website = websiteCur.getString(websiteCur.getColumnIndex(  
  223.                             ContactsContract.CommonDataKinds.Website.URL));  
  224.                 data.put(KEY_WEBSITE, website);  
  225.             }  
  226.         } finally {  
  227.             if (websiteCur != null) {  
  228.                 websiteCur.close();  
  229.             }  
  230.         }  
  231.     }  
  232.       
  233.     private void getIM (String contactId, JSONObject data) throws JSONException {  
  234.         Cursor imCur = null;  
  235.         try {  
  236.             String imWhere =   
  237.                 ContactsContract.Data.CONTACT_ID + " = ? AND " +   
  238.                 ContactsContract.Data.MIMETYPE + " = ?";  
  239.               
  240.             String[] imWhereParams = new String[]{contactId,  
  241.                     ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE};  
  242.               
  243.             imCur = context.getContentResolver().query(  
  244.                     ContactsContract.Data.CONTENT_URI,  
  245.                     null,   
  246.                     imWhere,   
  247.                     imWhereParams,   
  248.                     null);  
  249.               
  250.             JSONArray imList = new JSONArray();  
  251.             while (imCur.moveToNext()) {  
  252.                 String imName = imCur.getString(  
  253.                          imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));  
  254.                 int type = imCur.getInt(  
  255.                          imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));  
  256.                 String imType = ContactsContract.CommonDataKinds.Im.getTypeLabel(  
  257.                             context.getResources(), type, "").toString();  
  258.                   
  259.                 JSONObject item = new JSONObject();  
  260.                 item.put("imName", imName);  
  261.                 item.put("imType", imType);  
  262.                   
  263.                 imList.put(item);  
  264.             }  
  265.               
  266.             data.put(KEY_IM, imList);  
  267.         } finally {  
  268.             if (imCur != null) {  
  269.                 imCur.close();  
  270.             }  
  271.         }  
  272.     }  
  273.       
  274.     private void getOrg (String contactId, JSONObject data) throws JSONException {  
  275.         Cursor orgCur = null;  
  276.         try {  
  277.             String orgWhere =   
  278.                 ContactsContract.Data.CONTACT_ID + " = ? AND " +   
  279.                 ContactsContract.Data.MIMETYPE + " = ?";  
  280.               
  281.             String[] orgWhereParams = new String[]{contactId,  
  282.                     ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};  
  283.               
  284.             orgCur = context.getContentResolver().query(  
  285.                     ContactsContract.Data.CONTENT_URI,  
  286.                     null,   
  287.                     orgWhere,   
  288.                     orgWhereParams,   
  289.                     null);  
  290.             JSONArray orgList = new JSONArray();  
  291.             while (orgCur.moveToNext()) {  
  292.                 String orgName = orgCur.getString(orgCur.getColumnIndex(  
  293.                             ContactsContract.CommonDataKinds.Organization.DATA));  
  294.                 String title = orgCur.getString(orgCur.getColumnIndex(  
  295.                         ContactsContract.CommonDataKinds.Organization.TITLE));  
  296.                   
  297.                 JSONObject item = new JSONObject();  
  298.                 item.put("orgName", orgName);  
  299.                 item.put("title", title);  
  300.                   
  301.                 orgList.put(item);  
  302.             }  
  303.             data.put(KEY_ORG, orgList);  
  304.         } finally {  
  305.           if (orgCur != null) {  
  306.               orgCur.close();  
  307.           }  
  308.         }  
  309.     }  
  310.       
  311.     private void getNickname (String contactId, JSONObject data) throws JSONException {  
  312.         Cursor nicknameCur = null;  
  313.         try {  
  314.             String nicknameWhere =   
  315.                 ContactsContract.Data.CONTACT_ID + " = ? AND " +   
  316.                 ContactsContract.Data.MIMETYPE + " = ?";  
  317.               
  318.             String[] nicknameWhereParams = new String[]{contactId,  
  319.                     ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE};  
  320.                                       
  321.             nicknameCur = context.getContentResolver().query(  
  322.                     ContactsContract.Data.CONTENT_URI,  
  323.                     null,   
  324.                     nicknameWhere,   
  325.                     nicknameWhereParams,   
  326.                     null);  
  327.               
  328.             while (nicknameCur.moveToNext()) {  
  329.                 String nickname = nicknameCur.getString(nicknameCur.getColumnIndex(  
  330.                                     ContactsContract.CommonDataKinds.Nickname.NAME));  
  331.                 data.put(KEY_NICKNAME, nickname);  
  332.                 break;  
  333.             }  
  334.         } finally {  
  335.             if (nicknameCur != null) {  
  336.                 nicknameCur.close();  
  337.             }  
  338.         }  
  339.     }  
  340.   
  341.     private void getBirthday (String contactId, JSONObject data) throws JSONException {  
  342.         Cursor bCur = null;  
  343.         try {  
  344.             bCur = context.getContentResolver().query(  
  345.                     ContactsContract.Data.CONTENT_URI,   
  346.                     new String[] {ContactsContract.CommonDataKinds.Event.DATA },   
  347.                     ContactsContract.Data.CONTACT_ID+" = "+contactId+" AND "  
  348.                     +ContactsContract.Data.MIMETYPE+" = '"  
  349.                     +ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE+"' AND "  
  350.                     +ContactsContract.CommonDataKinds.Event.TYPE+" = "  
  351.                     +ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY,   
  352.                     null,   
  353.                     null);  
  354.             while (bCur.moveToNext()) {  
  355.                 String birthday = bCur.getString(0);  
  356.                 data.put(KEY_BIRTH, birthday);  
  357.                 break;  
  358.             }  
  359.         } finally {  
  360.             if (bCur != null) {  
  361.                 bCur.close();  
  362.             }  
  363.         }  
  364.     }  
  365.       
  366.     /** 
  367.      * Get address infomation of given contact. 
  368.      *  
  369.      * @param contactId 
  370.      * @param data 
  371.      * @throws JSONException 
  372.      */  
  373.     private void getAddress (String contactId, JSONObject data) throws JSONException {  
  374.           
  375.         Cursor postals = null;  
  376.         try {  
  377.             // address  
  378.             postals = context.getContentResolver().query(  
  379.                     ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,  
  380.                     null,  
  381.                     ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + contactId,   
  382.                     null,   
  383.                     null);  
  384.               
  385.             int postFormattedNdx = postals.getColumnIndex(  
  386.                         ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);  
  387.             int postTypeNdx = postals.getColumnIndex(  
  388.                         ContactsContract.CommonDataKinds.StructuredPostal.TYPE);  
  389.             int postStreetNdx = postals.getColumnIndex(  
  390.                         ContactsContract.CommonDataKinds.StructuredPostal.STREET);  
  391.               
  392.             JSONArray addrList = new JSONArray();  
  393.             while (postals.moveToNext()) {  
  394.                 String addressType = ContactsContract.CommonDataKinds.StructuredPostal  
  395.                             .getTypeLabel(context.getResources(), postTypeNdx, "").toString();  
  396.                 String str1 = postals.getString(postFormattedNdx);  
  397.                 String str2 = postals.getString(postStreetNdx);  
  398.                   
  399.                 JSONObject item = new JSONObject();  
  400.                 item.put("addressType", addressType);  
  401.                 item.put("address", str1 + str2);  
  402.                   
  403.                 addrList.put(item);  
  404.             }   
  405.               
  406.             data.put(KEY_ADDR, addrList);  
  407.         } finally {  
  408.             if (postals != null) {  
  409.                 postals.close();  
  410.             }  
  411.         }  
  412.     }  
  413.       
  414.     /** 
  415.      * Get the photo of given contact. 
  416.      *  
  417.      * @param cr 
  418.      * @param id 
  419.      * @param photo_id 
  420.      * @return 
  421.      */  
  422.     private void getPhoto (String contactId, long photoId, JSONObject data) throws JSONException {  
  423.           
  424.         Uri uri = ContentUris.withAppendedId(  
  425.                 ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId));  
  426.           
  427.         InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(  
  428.                     context.getContentResolver(), uri);  
  429.         if (input != null) {  
  430.             /*Bitmap photo =  BitmapFactory.decodeStream(input); 
  431.             data.put(KEY_PHOTO, photo);*/  
  432.         } else {  
  433.             Log.d(TAG, "First try failed to load photo!");  
  434.         }  
  435.   
  436.         byte[] photoBytes = null;  
  437.         Uri photoUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, photoId);  
  438.         Cursor c = context.getContentResolver().query(  
  439.                 photoUri,   
  440.                 new String[] {ContactsContract.CommonDataKinds.Photo.PHOTO},  
  441.                 null,   
  442.                 null,   
  443.                 null);  
  444.         try {  
  445.             if (c.moveToFirst()) {  
  446.                 photoBytes = c.getBlob(0);  
  447.             }  
  448.         } catch (Exception e) {  
  449.             e.printStackTrace();  
  450.         } finally {  
  451.             c.close();  
  452.         }  
  453.   
  454.         if (photoBytes != null) {  
  455.             /*Bitmap photo = BitmapFactory.decodeByteArray(photoBytes, 0, photoBytes.length); 
  456.             data.put(KEY_PHOTO, photo);*/  
  457.         } else {  
  458.             Log.d(TAG, "Second try also failed!");  
  459.         }  
  460.     }  
  461.   
  462.   
  463. }  


 

结果如下:

[javascript] view plain copy
 
    1. {  
    2.     "TIMESTAMP": 946692989880,  
    3.     "CONTACTS": [  
    4.         {  
    5.             "id": "801",  
    6.             "phone": [  
    7.                 {  
    8.                     "type": "手机",  
    9.                     "phone": "11122"  
    10.                 },  
    11.                 {  
    12.                     "type": "家",  
    13.                     "phone": "222111"  
    14.                 }  
    15.             ],  
    16.             "org": [],  
    17.             "IM": [],  
    18.             "address": [],  
    19.             "email": [  
    20.                 {  
    21.                     "type": "工作",  
    22.                     "email": "zhangsan@qq.com"  
    23.                 }  
    24.             ],  
    25.             "name": "john"  
    26.         },  
    27.         {  
    28.             "id": "802",  
    29.             "org": [],  
    30.             "IM": [],  
    31.             "address": [],  
    32.             "email": [],  
    33.             "name": "jack"  
    34.         },  
    35.         {  
    36.             "id": "803",  
    37.             "phone": [  
    38.                 {  
    39.                     "type": "手机",  
    40.                     "phone": "1300070302533"  
    41.                 }  
    42.             ],  
    43.             "org": [],  
    44.             "IM": [],  
    45.             "address": [],  
    46.             "email": [],  
    47.             "name": "alex"  
    48.         },  
    49.         {  
    50.             "id": "1064",  
    51.             "birthday": "2000-01-06",  
    52.             "phone": [  
    53.                 {  
    54.                     "type": "手机",  
    55.                     "phone": "18811112222"  
    56.                 },  
    57.                 {  
    58.                     "type": "家",  
    59.                     "phone": "18811113333"  
    60.                 },  
    61.                 {  
    62.                     "type": "工作",  
    63.                     "phone": "18811114444"  
    64.                 }  
    65.             ],  
    66.             "IM": [  
    67.                 {  
    68.                     "imName": "12345678",  
    69.                     "imType": "其他"  
    70.                 },  
    71.                 {  
    72.                     "imName": "13245678",  
    73.                     "imType": "其他"  
    74.                 }  
    75.             ],  
    76.             "website": "www.baidu.com",  
    77.             "nickname": "nickname",  
    78.             "address": [  
    79.                 {  
    80.                     "address": "百度市谷歌西路10086号",  
    81.                     "addressType": "自定义"  
    82.                 }  
    83.             ],  
    84.             "email": [  
    85.                 {  
    86.                     "type": "工作",  
    87.                     "email": "office@xxx.com"  
    88.                 },  
    89.                 {  
    90.                     "type": "住宅",  
    91.                     "email": "home@xxx.com"  
    92.                 }  
    93.             ],  
    94.             "name": "小强",  
    95.             "org": [  
    96.                 {  
    97.                     "orgName": "三月",  
    98.                     "title": "攻城狮"  
    99.                 }  
    100.             ],  
    101.             "note": "comment  for test"  
    102.         }  
    103.     ]  
    104. }  

posted on 2016-03-10 16:20  antyi  阅读(273)  评论(0编辑  收藏  举报

导航