使用下面的代码:
public void readContacts(){
ContentResolver cr = parent.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
Contact tempcontact=new Contact();
contactsList.add(tempcontact);
String id=cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
tempcontact.setId(cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)));
tempcontact.setName(name);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Log.e("DATA","name : " + name + ", ID : " + id);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
if(pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))!=null)
{
tempcontact.getPhonenumber().add(pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
// System.out.println("phone" + phone);
// Log.e("phone",phone);
}
pCur.close();
// get email and type
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
tempcontact.getEmail().add(emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
Log.e("Data","Email " + email + " Email Type : " + emailType);
}
emailCur.close();
String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] addrWhereParams = new String[]{id,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI,
null, null, null, null);
while(addrCur.moveToNext()) {
tempcontact.setPoBox((addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX))));
tempcontact.setPoBox(addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)));
tempcontact.setCity(addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)));
tempcontact.setRegion(addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION)));
tempcontact.setPostalCode(addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE)));
tempcontact.setCountry((addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY))));
tempcontact.setType(addrCur.getString(
addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE)));
// Do something with these....
}
addrCur.close();
String[] projection = {ContactsContract.CommonDataKinds.Photo.PHOTO};
Uri uri = (Uri)ContactsContract.Data.CONTENT_URI;
String where = ContactsContract.Data.MIMETYPE
+ "=" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + " AND "
+ ContactsContract.Data.CONTACT_ID + " = " + id;
Cursor cursor = parent.getContentResolver().query(uri, projection, null, null, null);
if(cursor!=null&&cursor.moveToFirst()){
do{
byte[] photoData = cursor.getBlob(0);
tempcontact.setPhoto(photoData);
//Do whatever with your photo here...
}while(cursor.moveToNext());
}
}
}
}
}
public class Contact {
String id;
String name;
ArrayList<String> phonenumber;
ArrayList<String> email;
String poBox;
String street;
String city;
String state;
String postalCode;
String country;
String type;
String region;
byte[] photo;
public byte[] getPhoto() {
return photo;
}
public Contact() {
// TODO Auto-generated constructor stub
phonenumber=new ArrayList<String>();
email=new ArrayList<String>();
}
public void setPhoto(byte[] photo) {
this.photo = photo;
}
public String getPoBox() {
return poBox;
}
public void setPoBox(String poBox) {
this.poBox = poBox;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<String> getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(ArrayList<String> phonenumber) {
this.phonenumber = phonenumber;
}
public ArrayList<String> getEmail() {
return email;
}
public void setEmail(ArrayList<String> email) {
this.email = email;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
}