2017-02-09 67 views
19

我想獲得WhatsApp個人資料圖片和編號,但使用contentResolver我只會使用以下代碼片段獲取姓名和電話號碼。我想獲得whatsApp profilepicture,但只獲取名稱和聯繫人號碼?

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聯繫人名單,號碼和縮略圖。

回答

4

首先,我只想重申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); 
+0

這是不適用於OP? –

相關問題