2017-06-19 82 views
1

我正在使用下面的代码来访问电话号码和联系人的姓名。我想添加到这个功能的方式来添加联系人的照片。我想检索我的联系人的联系人照片。我如何去做呢?

void loadContacts() { 
    ContentResolver contentResolver=getContentResolver(); 
    Cursor cursor=contentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null); 
    if(cursor.getCount() > 0) { 
     while (cursor.moveToNext()) {String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
      String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
      int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); 
      if (hasPhoneNumber > 0) { 
       Cursor cursor2 = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null); 
       while (cursor2.moveToNext()) { 
        String phoneNumber = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        make_contact(name,phoneNumber); 
       } 
       cursor2.close(); 
      } 
     } 
    } 
    cursor.close(); 
} 
+0

按照此链接https://gist.github.com/evandrix/7058235 –

回答

1

对于位图图像,

private Bitmap retrieveContactPhoto(String contactID) { 

      Bitmap photo = null; 

      try { 
       InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), 
         ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID))); 

       if (inputStream != null) { 
        photo = BitmapFactory.decodeStream(inputStream); 
       } 

       assert inputStream != null; 
       inputStream.close(); 

       return photo ; 

      } catch (IOException e) { 
       e.printStackTrace(); 
       return null; 
      } 

     } 

通过使用联系人ID,您可以检索位图图像并设置为imageview。

+0

使用此Long.valueOf(contactID),而不是这个新的长(contactID)。 – RameshJaga

+0

此代码在关闭inputStream.close()时创建NullPointerException;在关闭IOstream之前进行空检查。 – RameshJaga

+0

您的代码读取联系人图片并正常工作。 – RameshJaga

0

试试这个

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