2014-08-29 165 views
0

我需要您的帮助才能获取我的联系人照片。Android没有找到联系人照片

我收到我的所有联系人的生日。现在我必须试着像的人的照片:

... 
while (contactCursor.moveToNext()) { 
    Contact c = Contact.createContact(contactCursor); 
    c.setPhoto(getContactPhoto(contentResolver, c.getId())); 
    contacts.add(c); 
} 
.. 

private static InputStream getContactPhoto(ContentResolver contentResolver, long contactId) { 
     Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId); 
     Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 
     Cursor cursor = contentResolver.query(photoUri, new String[] {ContactsContract.Contacts.Photo.PHOTO}, null, null, null); 
     if (cursor == null) { 
      return null; 
     } 
     try { 
      if (cursor.moveToFirst()) { 
       byte[] data = cursor.getBlob(0); 
       if (data != null) { 
        return new ByteArrayInputStream(data); 
       } 
      } 
     } finally { 
      cursor.close(); 
     } 
     return null; 
    } 

我的问题是现在我不能接收联系人的任何照片(cursos大小= 0)。在手机上(Xperia Z1 Compact)绝对是联系照片。

我的目标是再设置这样的照片:

if(contact.getPhoto() != null) { 
     Bitmap cPhoto = BitmapFactory.decodeStream(contact.getPhoto()); 
     holder.photo.setImageBitmap(cPhoto); 
    } else { 
     ShapeDrawable d = new ShapeDrawable(new OvalShape()); 
     d.getPaint().setColor(Color.BLUE); 
     d.setBounds(10, 10, 20, 20); 
     holder.photo.setImageDrawable(d); 
    } 

非常感谢您的帮助。

回答

0

尝试使用毕加索

Picasso.with(getApplicationContext()) 
       .load(Uri.parse(photoUri)).noFade() 
       .into(imageView); 
+0

谢谢您的回答这个

public void getContacts(ContentResolver cr) { Cursor cursor = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { String name = cursor .getString(cursor .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String photoUri = cursor .getString(cursor .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)); friendsListitem = new FriendsListitem(name, photoUri, phoneNumber); arrayListFriends.add(friendsListitem); } cursor.close();// close cursor } 

负荷图像。但我会试着理解我的错。 对于同一联系人,我有不同的照片URI: - getContactPhoto:photoUri is:content://com.android.contacts/contacts/9444/photo - contact property:ContactsContract.Contacts.PHOTO_URI is:content:// com .android.contacts/contacts/243/photo 所以我无法理解这些行为。照片来自何处? Google,FB等? – bluepix 2014-08-29 15:11:46