gpt4 book ai didi

java - 我想检索联系人的联系人照片。我该怎么做?

转载 作者:行者123 更新时间:2023-11-30 00:29:48 26 4
gpt4 key购买 nike

我正在使用下面的代码访问联系人的电话号码和姓名。我想在此功能中添加一种添加联系人照片的方法。

void loadContacts() {
ContentResolver contentResolver=getContentResolver();
Cursor cursor=contentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
if(cursor.getCount() > 0) {
while (cursor.moveToNext()) {String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
Cursor cursor2 = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
while (cursor2.moveToNext()) {
String phoneNumber = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
make_contact(name,phoneNumber);
}
cursor2.close();
}
}
}
cursor.close();
}

最佳答案

对于位图图像,

private Bitmap retrieveContactPhoto(String contactID) {

Bitmap photo = null;

try {
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID)));

if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
}

assert inputStream != null;
inputStream.close();

return photo ;

} catch (IOException e) {
e.printStackTrace();
return null;
}

}

通过使用联系人 ID,您可以检索位图图像并设置为 imageview。

关于java - 我想检索联系人的联系人照片。我该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44627408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com