0

Creating an app in which I am fetching all device contact in recyclerview using the content provider. My all contact and contact name is coming but contact image is not showing.

My Image is null but the image is set on my device contact. Here what I have done.

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);

            }
}
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
user7108398
  • 91
  • 1
  • 1
  • 8

1 Answers1

0

If you have contactID then you can easily get contact image by appending contact id to Contacts.Photo.CONTENT_DIRECTORY of contacts

 private Uri retrieveContactPhoto(String id) {

    return Uri.withAppendedPath(ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id)),
            ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}

And then use Glide of Picasso to load contact image into imageview

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

  Glide.with(this).load(retrieveContactPhoto(someID)).into(imageView);
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
  • i am using ImageLoader and i want to show in recyclerview so i have to set image loader in adapter and in mainfragment i have to set contentprovider,so how to set this method in fragment in content provider? – user7108398 Feb 01 '17 at 08:09
  • add a field to store image uri in Contact class and set uri in your getAllContacts method :- contactVO.setContactImage(retrieveContactPhoto(someID)); – Hitesh Sahu Feb 01 '17 at 10:56