2012-10-23 35 views
4

我讓用戶在我的應用程序中選擇一個聯繫人,並將其顯示在主屏幕上的小部件中,但照片不顯示,我不知道錯在哪裏。通過photo_ID顯示聯繫人的照片

這是我如何得到參考照片:

... 
Cursor c = null; 
try { 
    c = getContentResolver().query(uri, new String[] { 
      ContactsContract.CommonDataKinds.Phone.NUMBER, 
      ContactsContract.CommonDataKinds.Phone.TYPE, 
      ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
      ContactsContract.CommonDataKinds.Phone.PHOTO_ID }, 
      null, null, null); 

    if (c != null && c.moveToFirst()) { 
     String number = c.getString(0); 
     int type = c.getInt(1); 
     String name = c.getString(2); 
     int photo = c.getInt(3); 
     showSelectedNumber(type, number, name, photo); 
    } 
} 

這是我如何顯示它:

public void showSelectedNumber(int type, String number, String name, int photo) { 
    mAppWidgetPrefix.setText(name); 
    pickedNumber.setText(number); 
    pickedPhoto.setImageResource(photo); 
} 

爲什麼它不工作?

+0

爲什麼沒有錯誤日誌? – ariefbayu

+0

檢查此問題http://stackoverflow.com/questions/6614757/android-get-contact-picture-from-call-log – juned

+0

有沒有錯誤。該照片根本不顯示。什麼都不顯示。 – keybee

回答

11

您試圖將ContactsContract.Data表中的行的ID設置爲您的ImageView中的資源ID。當然,這是行不通的。它甚至沒有任何意義。

您應該先從數據庫中檢索原始照片,然後才能顯示它。

例如,您可以使用此代碼來獲取圖像的位圖與行ID指向的幫助下,將圖像數據(我重新的一些代碼片段只是爲了測試它):

private void queryContactInfo(int rawContactId) { 
    Cursor c = getContentResolver().query(
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
      new String[] { 
        ContactsContract.CommonDataKinds.Phone.NUMBER, 
        ContactsContract.CommonDataKinds.Phone.TYPE, 
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
        ContactsContract.CommonDataKinds.Phone.PHOTO_ID 
      }, ContactsContract.Data.RAW_CONTACT_ID + "=?", new String[] { Integer.toString(rawContactId) }, null); 
    if (c != null) { 
     if (c.moveToFirst()) { 
      String number = c.getString(0); 
      int type = c.getInt(1); 
      String name = c.getString(2); 
      int photoId = c.getInt(3); 
      Bitmap bitmap = queryContactImage(photoId); 
      showSelectedNumber(type, number, name, bitmap); 
     } 
     c.close(); 
    } 
} 

private Bitmap queryContactImage(int imageDataRow) { 
    Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] { 
     ContactsContract.CommonDataKinds.Photo.PHOTO 
    }, ContactsContract.Data._ID + "=?", new String[] { 
     Integer.toString(imageDataRow) 
    }, null); 
    byte[] imageBytes = null; 
    if (c != null) { 
     if (c.moveToFirst()) { 
      imageBytes = c.getBlob(0); 
     } 
     c.close(); 
    } 

    if (imageBytes != null) { 
     return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); 
    } else { 
     return null; 
    } 
} 

public void showSelectedNumber(int type, String number, String name, Bitmap bitmap) { 
    mInfoView.setText(type + " " + number + " " + name); 
    mImageView.setImageBitmap(bitmap); // null-safe 
} 

你也可以將http://developer.android.com/reference/android/provider/ContactsContract.Contacts.Photo.html 作爲獲取聯繫人照片的便捷提供商目錄。也有一個例子。