2017-02-01 28 views
0

創建一個應用程序,使用內容提供程序在recyclerview中獲取所有設備聯繫人。我的所有聯繫人和聯繫人姓名即將到來,但未顯示聯繫人圖片。使用contentprovider在RecyclerView中獲取聯繫人圖像

我的圖像爲空,但圖像設置在我的設備聯繫人上。 這裏我做了什麼。

private void getAllContacts() { 

    ContentResolver contentResolver = getActivity().getContentResolver(); 
    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); 
    if (cursor.getCount() > 0) { 
     while (cursor.moveToNext()) { 

      int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); 
      if (hasPhoneNumber > 0) { 
       String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
       String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
       String image_uri = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_URI)); 


       contactVO = new Contact(); 
       contactVO.setContactImage(image_uri); 
       contactVO.setContactName(name); 

       /*if (image_uri != null) { 
        image.setImageURI(Uri.parse(image_uri)); 
       }*/ 

       phoneCursor = contentResolver.query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", 
         new String[]{id}, 
         null); 

       if (phoneCursor.moveToNext()) { 
        phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
         contactVO.setContactNumber(phoneNumber); 

        if (Objects.equals(phoneNumber, mobile)) { 
         contactArrayList.add(contact); 
        } 
       } 

       phoneCursor.close(); 
      //object of model class 
      contactArrayList.add(contactVO); 

      } 
} 

回答

0

如果你有contactID那麼你可以通過附加接觸式ID輕鬆搞定接觸式圖像聯繫人

private Uri retrieveContactPhoto(String id) { 

    return Uri.withAppendedPath(ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id)), 
      ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 
} 

然後使用PicassoGlideContacts.Photo.CONTENT_DIRECTORY加載接觸式圖像到imageview的

ImageView imageView = (ImageView) findViewById(R.id.my_image_view); 

    Glide.with(this).load(retrieveContactPhoto(someID)).into(imageView); 
+0

我正在使用ImageLoader,我想在recyclerview中顯示,所以我必須設置圖像加載器在適配器和mainfragment我必須設置contentprovider,所以如何在片段中設置此方法在內容提供商? – user7108398

+0

在Contact類中添加一個字段來存儲圖像uri,並在getAllContacts方法中設置uri: - contactVO.setContactImage(retrieveContactPhoto(someID)); –

0

您可以試試這個: 聯繫人由getId()標識:

/** 
* @return the photo URI 
*/ 
public Uri getPhotoUri() { 
    try { 
     Cursor cur = this.ctx.getContentResolver().query(
       ContactsContract.Data.CONTENT_URI, 
       null, 
       ContactsContract.Data.CONTACT_ID + "=" + this.getId() + " AND " 
         + ContactsContract.Data.MIMETYPE + "='" 
         + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null, 
       null); 
     if (cur != null) { 
      if (!cur.moveToFirst()) { 
       return null; // no photo 
      } 
     } else { 
      return null; // error in cursor process 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } 
    Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long 
      .parseLong(getId())); 
    return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 
} 

用法爲:

Uri u = objItem.getPhotoUri(); 
if (u != null) { 
     mPhotoView.setImageURI(u); 
} else { 
     mPhotoView.setImageResource(R.drawable.ic_contact_picture_2); 
} 

希望這有助於你。

+0

getPhotoUri()應該是靜態的或不是,它顯示錯誤。 – user7108398

+0

您可以將getPhotoUri()方法放置在您的Utils類中,並將其設置爲靜態,否則如果您只需要在一個位置或活動中,則可以將該方法保留在該類中。 –

+0

我正在使用這回收器查看適配器,但顯示錯誤 – user7108398