【问题标题】:How to get contact images from recent call information in Android如何从 Android 中的最近通话信息中获取联系人图片
【发布时间】:2016-08-17 17:39:26
【问题描述】:

我的用例很简单:我想在标准 android 手机应用中创建一个类似于最近通话列表视图的列表视图。

我可以使用 getContentResolvery().query() 在 android.provider.CallLog.Calls 中查询最近的通话...问题是我还需要联系人图片,如果该联系人存在于用户联系人列表中。 ..我相信这将是从 CallLog.Calls 加入到其他提供商,也许是 ContactContract 提供商?

理想情况下,我会在一个光标中收到此信息。

感谢您的帮助

【问题讨论】:

    标签: android join android-contentprovider


    【解决方案1】:

    使用联系人 ID 获取联系人照片 URI。使用以下代码获取照片 URI:

    import android.provider.ContactsContract;
    import android.provider.ContactsContract.CommonDataKinds.Phone;
    
    public Uri getPhotoUri(long contactId) {
        ContentResolver contentResolver = getContentResolver();
    
        try {
            Cursor cursor = contentResolver.query(ContactsContract.Data.CONTENT_URI,null,ContactsContract.Data.CONTACT_ID+ "="+ contactId+ " AND "+ ContactsContract.Data.MIMETYPE+"='"+ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE+ "'", null, null);
    
            if (cursor != null) {
            if (!cursor.moveToFirst()) {
                return null; // no photo
            }
            } else {
            return null; // error in cursor process
            }
    
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    
        Uri person = ContentUris.withAppendedId(
            ContactsContract.Contacts.CONTENT_URI, contactId);
        return Uri.withAppendedPath(person,ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
      }
    

    更新:

    联系人id可以通过电话号码获取

    import android.provider.ContactsContract.PhoneLookup;
    
    public String fetchContactIdFromPhoneNumber(String phoneNumber) {
        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phoneNumber));
        Cursor cursor = this.getContentResolver().query(uri,new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID },null, null, null);
    
        String contactId = "";
    
        if (cursor.moveToFirst()) {
            do {
            contactId = cursor.getString(cursor.getColumnIndex(PhoneLookup._ID));
            } while (cursor.moveToNext());
        }
    
        return contactId;
      }
    

    【讨论】:

    • 不幸的是,CallLog.Calls 内容提供程序中没有contactID。我认为 CallLogs.Call 和 ContactsContract 之间唯一的共同字段可能是电话号码或姓名……我们可以使用这些字段来调整您的解决方案吗?
    • 可以使用手机号获取id
    • 使用 ContactsContract.PhoneLookup
    猜你喜欢
    • 2011-09-30
    • 2012-02-21
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    相关资源
    最近更新 更多