0

I am trying to get WhatsApp profile photos and i am getting photoUri for all my contacts that have WhatsApp.

but when am trying to present the photoUri i am getting the following exception:

W/ImageView: Unable to open content: content://com.android.contacts/raw_contacts/12/display_photo
java.io.FileNotFoundException: No photo file found for ID 0

I add a code to check if the file is existing or not and i have found that the file is existing, so it is not clear why i am getting the above exception.

here is the code that i am using to get the photo URI:

public Uri getPhotoUri(long contactId) {

Uri photoUri=null;

ContentResolver contentResolver = getContext().getContentResolver();
Cursor photoCur = contentResolver.query(
        ContactsContract.RawContacts.CONTENT_URI,null,
        ContactsContract.RawContacts.CONTACT_ID + "=" + contactId + " AND " +
                ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?",
        new String[]{"com.whatsapp"}, ContactsContract.RawContacts.CONTACT_ID);

if (photoCur != null && photoCur.moveToFirst()) {
    Uri photoId = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, contactId);
    photoUri = Uri.withAppendedPath(photoId, ContactsContract.RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
}

return photoUri;
}

here is the code that i am using to check if the file exists or not:

    if (customer.getContactPhotoUri() != null) {

    String fullPath = customer.getContactPhotoUri().toString();
    File path = new File(fullPath);
    if (path.exists()) {
        Log.i("ZCF", "File Exists: " + fullPath);
        imageView.setImageURI(customer.getContactPhotoUri());
    } else {
        Log.i("ZCF", "File does not Exists: " + fullPath);
        imageView.setImageDrawable(context.getDrawable(R.drawable.ic_person_blue_24dp));
    }
} else {
    imageView.setImageDrawable(context.getDrawable(R.drawable.ic_person_blue_24dp));
}

the result of this code is:

I/ZCF: File Exists: content://com.android.contacts/raw_contacts/12/display_photo

Zion Cohen
  • 182
  • 6
  • 19

1 Answers1

1

Whatsapp doesn't store its contact photos using ContactsContract, so you can't access them.

The photos are stored within Whatsapp's folder on your internal memory drive, you can see them manually using a file-explorer app, but these folders are not accessible to apps, so you can't access them programatically.

This is obviously intentional, as Whatsapp doesn't want their photos to be leaked out outside of their app.

marmor
  • 27,641
  • 11
  • 107
  • 150
  • thanks. do you know what is the whatsapp folder that all contacts pictures are saved on my device? – Zion Cohen Feb 11 '19 at 12:42
  • it's in `/Internal storage/WhatsApp/Media/WhatsApp Profile Photos`, it should only contain profile photos that the user clicked on to enlarge, not everything. but checking it now, it looks like it's empty on my device, so maybe it's in some other folder – marmor Feb 11 '19 at 13:45