Android 使用contentprovider在RecyclerView中获取联系人图像

Android 使用contentprovider在RecyclerView中获取联系人图像,android,android-recyclerview,android-contentprovider,Android,Android Recyclerview,Android Contentprovider,正在创建一个应用程序,其中我正在使用内容提供商获取recyclerview中的所有设备联系人。我的所有联系人和联系人姓名即将出现,但联系人图像未显示 我的映像为空,但已在我的设备联系人上设置映像。 这就是我所做的 private void getAllContacts() { ContentResolver contentResolver = getActivity().getContentResolver(); Cursor cursor = contentResolver.

正在创建一个应用程序,其中我正在使用内容提供商获取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);

            }
}

如果您有
contactID
,则可以通过将联系人id附加到联系人的
Contacts.Photo.CONTENT\u目录
,轻松获取联系人图像

 private Uri retrieveContactPhoto(String id) {

    return Uri.withAppendedPath(ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id)),
            ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
然后使用毕加索的Glide将联系人图像加载到imageview中

  ImageView imageView = (ImageView) findViewById(R.id.my_image_view);

  Glide.with(this).load(retrieveContactPhoto(someID)).into(imageView);
您可以尝试以下方法: 联系人由getId()标识:

用途是:

Uri u = objItem.getPhotoUri();
if (u != null) {
        mPhotoView.setImageURI(u);
} else {
        mPhotoView.setImageResource(R.drawable.ic_contact_picture_2);
}

希望这对您有所帮助。

我正在使用ImageLoader,我想在recyclerview中显示,因此我必须在adapter中设置ImageLoader,在mainfragment中,我必须设置contentprovider,那么如何在内容提供者的片段中设置此方法呢?添加一个字段以在Contact类中存储图像uri,并在getAllContacts方法中设置uri:-contactVO.setContactImage(retrieveContactPhoto(someID));getPhotoUri()应该是静态的还是非静态的,它正在显示错误。您可以将getPhotoUri()方法放置在Utils类中并使其成为静态的,否则,如果您只需要在一个位置或活动中,则可以将此方法保留在该类中。我在recyclerview适配器中使用此方法,但显示错误
Uri u = objItem.getPhotoUri();
if (u != null) {
        mPhotoView.setImageURI(u);
} else {
        mPhotoView.setImageResource(R.drawable.ic_contact_picture_2);
}