我想获取 whatsApp 个人资料图片,但只能获取姓名和联系电话?

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

我想获取 WhatsApp 个人资料图片和号码,但使用 contentResolver 我只能使用以下代码段获取姓名和号码。

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

我想像 SyncMe 应用一样获取 WhatsApp 个人资料图片。

我正在等待获取包含姓名、号码和缩略图的 WhatsApp 联系人列表。

首先我想重申一下 RawContactContact 之间的区别。后者是前者(代表一个账户)的集合体(代表一个人)。

根据图像的用途,获取聚合联系人并使用此处选择的个人资料图像可能更好,这可以通过 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);
}

如果您专门用于 WhatsApp 照片,请首先确保照片已缓存在设备上(只是为了避免在测试时出现错误标记 - 在 WhatsApp 中打开联系人完整图像,您可以确定它已缓存)然后您必须单独查找图像(来自 API 14):

Uri photoId = ContentUris.withAppendedId(RawContacts.CONTENT_URI, whatsappContactId);
Uri photoUri = Uri.withAppendedPath(photoId, RawContacts.DisplayPhoto.CONTENT_DIRECTORY);