我想获得whatsApp profilepicture但只获取名称和contactnumber?

Hir*_*ela 20 android android-contentresolver whatsapp

我想获取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());
}
Run Code Online (Sandbox Code Playgroud)

我想获得像SyncMe应用程序一样的WhatsApp个人资料图片.

我等着获得WhatsApp联系人列表,其中包含姓名,号码和缩略图.

Nic*_*oso 5

首先,我只想重申a RawContact和a 之间的区别Contact。后者是前者(代表帐户)的集合(代表一个人)。

根据您对图像的使用情况,最好提取总联系人并使用在其中选择的个人资料图像,这可以通过“ 照片”合同来实现。

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);
}
Run Code Online (Sandbox Code Playgroud)

如果您专门用于WhatsApp照片,请首先确保照片已缓存在设备上(为避免测试时出现虚假标志-在WhatsApp中打开联系人完整图像,可以确保已将其缓存),那么您必须对图像进行单独查找(来自API 14):

Uri photoId = ContentUris.withAppendedId(RawContacts.CONTENT_URI, whatsappContactId);
Uri photoUri = Uri.withAppendedPath(photoId, RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
Run Code Online (Sandbox Code Playgroud)