的Android从通话记录中

问题描述:

获取联系人图片这是很容易查询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; 
    } 

然后,你必须尝试通过查询通话记录的字段,以获得接触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

我做它通过这种方式:

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

这一个正常工作对我..

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); 
    } 
} 

尝试......

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; 
} 

以上所有答案都是正确的。您也可以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(); 
     }