【问题标题】:Android Get Contact Picture from Call LogAndroid 从通话记录中获取联系人图片
【发布时间】:2011-09-30 16:21:09
【问题描述】:

查询People.CONTENT_URI时很容易得到联系人图片,很简单

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+ 开始工作。

感谢任何帮助。谢谢。

按照下面 Cristian 的指导,对我有用的解决方案是:

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

【问题讨论】:

    标签: android contacts android-contacts calllog


    【解决方案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
    

    【讨论】:

    • 这个解决方案似乎正是我想要做的。唯一的问题是它没有返回正确的 id。例如,对于 id 为 110 的联系人,它返回 id 713。我确信 110 是正确的,因为它为 id 110 加载了正确的照片。我错过了什么吗?谢谢大家的支持。
    • 啊,如果我使用 Contacts.Phones.PERSON_ID 而不是使用 Contacts.Phones._ID,它可以工作。谢谢克里斯蒂安
    【解决方案2】:

    这个对我来说很好用..

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

    【讨论】:

      【解决方案3】:

      我是这样做的:

      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
      }
      

      【讨论】:

      • 但是如何从给定的某个电话号码中获取图片呢?
      • @Alin:不幸的是,给定的电话号码可以分配给多个联系人,甚至某些联系人可能根本没有电话,因此依靠电话号码获取图片确实是不可能的。我的意思是只能为给定的电话号码定义一组图片(可能是空的)。
      【解决方案4】:

      试试这个...

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

      【讨论】:

        【解决方案5】:

        以上答案都是正确的。你也可以通过这个来拍照...

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-02-05
          • 1970-01-01
          • 2023-03-28
          • 1970-01-01
          • 1970-01-01
          • 2015-04-01
          • 1970-01-01
          • 2014-01-19
          相关资源
          最近更新 更多