Android根据电话号码取得联系人姓名及头像



 2      * 根据电话号码取得联系人姓名
 3      */
 4     public static String getContactNameByPhoneNumber(Context context, String address) {
 5         String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
 6                 ContactsContract.CommonDataKinds.Phone.NUMBER };
 7
 8         // 将自己添加到 msPeers 中
 9         Cursor cursor = context.getContentResolver().query(
10                 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
11                 projection, // Which columns to return.
12                 ContactsContract.CommonDataKinds.Phone.NUMBER + " = '"
13                         + address + "'", // WHERE clause.
14                 null, // WHERE clause value substitution
15                 null); // Sort order.
16
17         if (cursor == null) {
18             Log.d(TAG, "getPeople null");
19             return null;
20         }
21         for (int i = 0; i < cursor.getCount(); i++) {
22             cursor.moveToPosition(i);
23
24             // 取得联系人名字
25             int nameFieldColumnIndex = cursor
26                     .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
27             String name = cursor.getString(nameFieldColumnIndex);
28             return name;
29         }
30         return null;
31     }
32
33 /**
34      * 获取所有联系人内容
35      * @param context
36      * @param address
37      * @return
38      */
39     public static String getContacts(Context context) {
40         StringBuilder sb = new StringBuilder();
41         
42         ContentResolver cr = context.getContentResolver();
43         Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
44                 null, null, null);
45
46         if (cursor.moveToFirst()) {
47             do {
48                 String contactId = cursor.getString(cursor
49                         .getColumnIndex(ContactsContract.Contacts._ID));
50                 String name = cursor
51                         .getString(cursor
52                                 .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
53                 //第一条不用换行
54                 if(sb.length() == 0){
55                     sb.append(name);
56                 }else{
57                     sb.append("\n" + name);
58                 }
59                 
60                 Cursor phones = cr.query(
61                         ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
62                         null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
63                                 + " = " + contactId, null, null);
64                 while (phones.moveToNext()) {
65                     String phoneNumber = phones
66                             .getString(phones
67                                     .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
68                     // 添加Phone的信息
69                     sb.append("\t").append(phoneNumber);
70                     
71                 }
72                 phones.close();
73                 
74             } while (cursor.moveToNext());
75         }
76         cursor.close();

77         return sb.toString();


联系人头像

  1. // 取得Intent中的頭像  
  2.             ivShowImage = (ImageView) findViewById(R.id.call_log_detail_contact_img);  
  3.             //通话电话号码获取头像uri  
  4.             Uri uriNumber2Contacts = Uri  
  5.                     .parse("content://com.android.contacts/"  
  6.                             + "data/phones/filter/" + strPhoneNumber);  
  7.             Cursor cursorCantacts = SApplication.nowSApplication  
  8.                     .getContentResolver().query(uriNumber2Contacts, nullnull,  
  9.                             nullnull);  
  10.             if (cursorCantacts.getCount() > 0) { //若游标不为0则说明有头像,游标指向第一条记录  
  11.                 cursorCantacts.moveToFirst();  
  12.                 Long contactID = cursorCantacts.getLong(cursorCantacts  
  13.                         .getColumnIndex("contact_id"));  
  14.                 Uri uri = ContentUris.withAppendedId(  
  15.                         ContactsContract.Contacts.CONTENT_URI, contactID);  
  16.                 InputStream input = ContactsContract.Contacts  
  17.                         .openContactPhotoInputStream(  
  18.                                 SApplication.nowSApplication  
  19.                                         .getContentResolver(), uri);  
  20.                 btContactImage = BitmapFactory.decodeStream(input);  
  21.                 Log.i("info""bt======" + btContactImage);  
  22.                 ivShowImage.setImageBitmap(btContactImage);  
  23.   
  24.             } else {//么有头像设置默认头像  
  25.                 ivShowImage  
  26.                         .setImageResource(R.drawable.call_log_contact_image2);  
  27.   
  28.             }  
  29.          

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值