2011-07-07 81 views
3

獲取聯繫人圖片這是很容易查詢People.CONTENT_URI時,爲了獲得聯繫人圖片,用一個簡單的的Android從通話記錄中

People.loadContactPhoto(activity, ContentUris.withAppendedId(People.CONTENT_URI, contactId) 

,因爲我知道聯繫人ID。現在我需要在訪問通話記錄後做同樣的事情。隨着:

String[] strFields = { 
       android.provider.CallLog.Calls.CACHED_NAME, 
       android.provider.CallLog.Calls.NUMBER, 
       }; 

     String strUriCalls="content://call_log/calls"; 

      Uri UriCalls = Uri.parse(strUriCalls); 

      Cursor cursorLog = this.getContentResolver().query(UriCalls, strFields, null, null, null); 

我從通話記錄中得到列表,但我找不到任何方式將此與需要加載照片的聯繫人ID鏈接。該應用程序從API級別4+起作用。

任何幫助表示讚賞。謝謝。

的解決方案,如克里斯蒂安以下,這對我工作的指導是:

private long getContactIdFromNumber(String number) { 
     String[] projection = new String[]{Contacts.Phones.PERSON_ID}; 
     Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,Uri.encode(number)); 
     Cursor c = getContentResolver().query(contactUri, projection, null, null, null); 

     if (c.moveToFirst()) { 
      long contactId=c.getLong(c.getColumnIndex(Contacts.Phones.PERSON_ID)); 
      return contactId; 
     } 
     return -1; 
    } 

回答

8

然後,你必須嘗試通過查詢通話記錄的字段,以獲得接觸ID。所以,你可以實現這樣的事情:

private String getContactIdFromNumber(String number) { 
    String[] projection = new String[]{Contacts.Phones._ID}; 
    Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, 
     Uri.encode(number)); 
    Cursor c = getContentResolver().query(contactUri, projection, 
     null, null, null); 
    if (c.moveToFirst()) { 
     String contactId=c.getString(c.getColumnIndex(Contacts.Phones._ID)); 
     return contactId; 
    } 
    return null; 
} 

然後,您可以使用接觸式ID來獲取照片。像這樣的事情在你的情況:

cursorLog.moveToFirst(); 
String number = cursorLog.getString(cursorLog.getColumnIndex(android.provider.CallLog.Calls.NUMBER)); 
contactId = getContactIdFromNumber(number) 
People.loadContactPhoto(activity, ContentUris.withAppendedId(People.CONTENT_URI, contactId); 
// blah blah blah 
+0

該解決方案似乎正是我想做的事情。唯一的問題是它不會返回正確的ID。例如,對於ID爲110的聯繫人,它返回ID爲713.我相信110是正確的,因爲它爲ID 110加載正確的照片。我是否錯過了什麼?謝謝大家的支持。 – Alin

+0

啊,而不是使用Contacts.Phones._ID,如果我使用Contacts.Phones.PERSON_ID它的作品。謝謝Cristian – Alin

0

我做它通過這種方式:

ContentResolver cr=this.getContentResolver(); 
Cursor cc = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
while (cc.moveToNext()) 
{ 
    contactId = cc.getString(cc.getColumnIndex(ContactsContract.Contacts._ID)); 
    Uri contactPhotoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId)); 
    InputStream is=ContactsContract.Contacts.openContactPhotoInputStream(cr, contactPhotoUri); 
    //blah-blah 
} 
+0

我認爲你的代碼中缺少一些東西... – Alin

+0

除了從InputStream中讀取位圖以外,所有東西都是一樣的 – barmaley

+0

但是,如何從給定的某個電話號碼中獲取圖片? – Alin

1

這一個正常工作對我..

private void contactPickedFromLog(Intent data) { 
    // TODO Auto-generated method stub 
    String contactNumber = data.getStringExtra(CONTACT_NUMBER); 
    Cursor cursor = getContentResolver().query(
      Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
        Uri.decode(contactNumber)), 
      new String[] { PhoneLookup._ID }, null, null, null); 
    if(cursor.moveToFirst()){ 
    long contactId = cursor.getLong(0); 
    InputStream inputStream = Contacts.openContactPhotoInputStream(
      getContentResolver(), 
      ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId)); 
    if(inputStream!=null) 
    Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
    } 
} 
0

嘗試......

public Bitmap getPhoto(String phoneNumber) { 
    Uri phoneUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); 
    Uri photoUri = null; 
    ContentResolver cr = getContentResolver(); 
    Cursor contact = cr.query(phoneUri, 
      new String[] { ContactsContract.Contacts._ID }, null, null, null); 

    if (contact.moveToFirst()) { 
     long userId = contact.getLong(contact.getColumnIndex(ContactsContract.Contacts._ID)); 
     photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, userId); 

    } 
    else { 
     Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture); 
     return getCircleBitmap(defaultPhoto); 
    } 
    if (photoUri != null) { 
     InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(
       cr, photoUri); 
     if (input != null) { 
      return getCircleBitmap(BitmapFactory.decodeStream(input)); 
     } 
    } else { 
     Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture); 
     return getCircleBitmap(defaultPhoto); 
    } 
    Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture); 
    contact.close(); 
    return defaultPhoto; 
} 
0

以上所有答案都是正確的。您也可以ge t photo by this ...

c.getString(c.getColumnIndex(CallLog.Calls.CACHED_PHOTO_URI));在SDK

工作> = 23

,如果您以最小的SDK工作...

Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(num)); 
     uri = ((phone_uri != null) ? Uri.parse(phone_uri) : uri); 
     Cursor cursor = getContext().getContentResolver().query(uri, null, null, null, null); 

     if (cursor != null) { 
      if (cursor.moveToNext()) { 
       String id = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID)); 
       String name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); 
       image_uri = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI)); 
       Log.d(TAG, "name " + name + " id "+id+" image_uri "+ image_uri); 
      } 
      cursor.close(); 
     }