2012-06-13 203 views
1

此代碼(在我的CustomAdapter類中)僅顯示基於誰發送給我的文本消息並將其放入ArrayList中的聯繫人ID,然後顯示該列表。如何顯示聯繫人ID的聯繫人照片?

我在每個聯繫人ID旁邊都有一個名爲holder.photo的ImageView。 我將如何去在ImageView中顯示聯繫人的照片?

 String folder = "content://sms/inbox/"; 
     Uri mSmsQueryUri = Uri.parse(folder); 
     messages = new ArrayList<String>(); 
     contactID = new ArrayList<String>(); 
     SMS = new ArrayList<String>(); 

     try { 
      c = context.getContentResolver().query(mSmsQueryUri, 
        new String[] { "_id", "address", "date", "body" }, 
        null, null, null); 
      if (c == null) { 
       Log.i(TAG, "cursor is null. uri: " + mSmsQueryUri); 
      } 

     } catch (Exception e) { 
      //Log.e(TAG, e.getMessage()); 
     } finally { 
      c.close(); 
     } 
      c.moveToFirst(); 
      while (c.moveToNext()) { 

       phoneNumber = c.getString(0); 
       contactID.add(phoneNumber); 
      } 
     holder.photo.????? 
     //contact will cycle through all names and display each in a listview. 
     holder.contact.setText(contactID.get(position); 

目前,我的列表視圖顯示此:

  • android_icon -----李四
  • android_icon -----珍妮·史密斯
  • android_icon -----富巴爾

回答

3

試試這個..

public void getContacts(ContentResolver cr) { 
    Cursor phones = cr.query(
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, 
      null, null); 
    while (phones.moveToNext()) { 
     String name = phones 
       .getString(phones 
         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
     String contactId = phones 
       .getString(phones 
         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); 

     Bitmap bitmap = loadContactPhoto(
       getContentResolver(), Long.valueOf(contactId)) 
     } 
    phones.close(); 

得到位圖圖像

public static Bitmap loadContactPhoto(ContentResolver cr, long id) { 
    Uri uri = ContentUris.withAppendedId(
      ContactsContract.Contacts.CONTENT_URI, id); 
    InputStream input = ContactsContract.Contacts 
      .openContactPhotoInputStream(cr, uri); 
    if (input == null) { 
     return null; 
    } 
    return BitmapFactory.decodeStream(input); 
} 
+0

它爲我工作三江源 –