創建一個應用程序,使用內容提供程序在recyclerview中獲取所有設備聯繫人。我的所有聯繫人和聯繫人姓名即將到來,但未顯示聯繫人圖片。使用contentprovider在RecyclerView中獲取聯繫人圖像
我的圖像爲空,但圖像設置在我的設備聯繫人上。 這裏我做了什麼。
private void getAllContacts() {
ContentResolver contentResolver = getActivity().getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String image_uri = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_URI));
contactVO = new Contact();
contactVO.setContactImage(image_uri);
contactVO.setContactName(name);
/*if (image_uri != null) {
image.setImageURI(Uri.parse(image_uri));
}*/
phoneCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id},
null);
if (phoneCursor.moveToNext()) {
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactVO.setContactNumber(phoneNumber);
if (Objects.equals(phoneNumber, mobile)) {
contactArrayList.add(contact);
}
}
phoneCursor.close();
//object of model class
contactArrayList.add(contactVO);
}
}
我正在使用ImageLoader,我想在recyclerview中顯示,所以我必須設置圖像加載器在適配器和mainfragment我必須設置contentprovider,所以如何在片段中設置此方法在內容提供商? – user7108398
在Contact類中添加一個字段來存儲圖像uri,並在getAllContacts方法中設置uri: - contactVO.setContactImage(retrieveContactPhoto(someID)); –