我仅使用他/她的电话号码检索联系人照片时遇到困难。 我使用" ContactsContract.Contacts.Photo"跟随官方Android示例。不幸的是,contactId参数是必需的。
我成功地使用以下代码设法检索图像:
long contactId = 0;
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = context.getContentResolver().query(uri,
new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID },
null, null, null);
if (cursor.moveToFirst()) {
contactId = cursor.getLong(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));
}
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
InputStream contactPhotoInputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), contactUri);
return BitmapFactory.decodeStream(contactPhotoInputStream);
但是,正如人们所看到的,我必须首先查询contactId。我现在的问题是,是否可以直接查询联系人的图片?
答案 0 :(得分:0)
我对此不确定,但您可以尝试
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.PHOTO_URI};
String where = ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?";
//You can use ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER
// depending on how the number is formatted
///the format of the phone number is important
String whereArgs = phoneNumber;
Cursor people = getContentResolver().query(uri, projection, where, whereArgs, null);