Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to get whatsApp profilepicture but only getting name and contactnumber?

Tags:

I want to get WhatsApp profile picture and number but using contentResolver I will getting only name and number using following snippet code.

private void showContactWhatsApp(){

    ContentResolver cr = getContentResolver();

    Cursor contactCursor = cr.query(
            ContactsContract.RawContacts.CONTENT_URI,
            new String[]{ContactsContract.RawContacts._ID,
                    ContactsContract.RawContacts.CONTACT_ID},
            ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?",
            new String[]{"com.whatsapp"},
            null);


    ArrayList<String> myWhatsappContacts = new ArrayList<>();

    if (contactCursor != null) {
        if (contactCursor.getCount() > 0) {
            if (contactCursor.moveToFirst()) {
                do {
                    //whatsappContactId for get Number,Name,Id ect... from  ContactsContract.CommonDataKinds.Phone
                    String whatsappContactId = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID));

                    if (whatsappContactId != null) {
                        //Get Data from ContactsContract.CommonDataKinds.Phone of Specific CONTACT_ID
                        Cursor whatsAppContactCursor = cr.query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                                        ContactsContract.CommonDataKinds.Phone.NUMBER,
                                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                                new String[]{whatsappContactId}, null);

                        if (whatsAppContactCursor != null) {
                            whatsAppContactCursor.moveToFirst();
                            String id = whatsAppContactCursor.getString(whatsAppContactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                            String name = whatsAppContactCursor.getString(whatsAppContactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                            String number = whatsAppContactCursor.getString(whatsAppContactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                            whatsAppContactCursor.close();

                            //Add Number to ArrayList
                            myWhatsappContacts.add(number);

                            Log.e(TAG, " WhatsApp contact id  :  " + id);
                            Log.e(TAG, " WhatsApp contact name :  " + name);
                            Log.e(TAG, " WhatsApp contact number :  " + number);
                        }
                    }
                } while (contactCursor.moveToNext());
                contactCursor.close();
            }
        }
    }

    Log.e(TAG, " WhatsApp contact size :  " + myWhatsappContacts.size());
}

I want to get WhatsApp profile picture like SyncMe app.

I wait to get WhatsApp contact list with name, number, and thumbnail.

like image 856
Hiren Vaghela Avatar asked Feb 09 '17 10:02

Hiren Vaghela


People also ask

Why can't I see some people's WhatsApp profile pictures?

If you can't see someone else's last seen, profile photo, about, status, or read receipts, it might be due to one of the following: There's a temporary network issue. You or the user changed your privacy settings for last seen or profile photo. You and the user both need to resync your contacts.

Can we see profile pic on WhatsApp if number is not saved?

WhatsApp profile pictures can be seen and saved by all WhatsApp users by simply taking a screenshot if it is not hidden. You might have spoken to certain people on WhatsApp who are not family or friends and are not on your contact list. If your profile photo is not hidden then anyone can see and save it if they want.

How can I set WhatsApp profile picture to selected contacts?

Launch WhatsApp. Go to Settings > Account > Privacy. Select Profile Photo. Choose My Contacts.


1 Answers

Firstly I just want to reiterate the difference between a RawContact and a Contact. The latter being an aggregation (representing a person) of the former (representing an account).

Depending on what use you have for the image, it may be better to fetch the aggregate Contact and use the Profile image selected there which can be achieved via the Photo contract.

Uri photoUri;
Cursor photoCur = cr.query(
    ContactsContract.Data.CONTENT_URI, null,
    ContactsContract.Data.CONTACT_ID + "=" + aggregateContactId + " AND " + 
    ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", 
    null, null
);
if (photoCur != null && photoCur.moveToFirst()) {
    Uri photoId = ContentUris.withAppendedId(Contacts.CONTENT_URI, aggregateContactId);
    photoUri = Uri.withAppendedPath(person, Contacts.Photo.CONTENT_DIRECTORY);
}

If you have use specifically for the WhatsApp photo, first make sure the photo is cached on the device (just to avoid false flags while testing - open the contacts full image in WhatsApp and you can be sure it's cached) then you'll have to do a separate lookup for the image (from API 14):

Uri photoId = ContentUris.withAppendedId(RawContacts.CONTENT_URI, whatsappContactId);
Uri photoUri = Uri.withAppendedPath(photoId, RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
like image 125
Nick Cardoso Avatar answered Sep 23 '22 04:09

Nick Cardoso