我想在移动联系人的列表视图中显示所有联系人详细信息(如显示标题名称,电话号码和图片)谁拥有手机号码名单。我已经获得了标题名称,电话号码。我按照this线程在列表中获得照片。但没有成功。
要获取显示标题名称,电话号码我使用以下方法。尝试使用相同的查询获取图像时出错。我可以在一个查询中获得所有查询,还是需要两个查询?
public static ArrayList<Person> getAllContactsWithPhoto(Context context) {
ArrayList<Person> allContacts = new ArrayList<Person>();
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.Data.CONTACT_ID + " = ?",
new String[] { id }, null);
while (pCur.moveToNext()) {
// fill object data
Person aPerson = new Person();
aPerson.setName(pCur.getString(pCur
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
aPerson.setNumber(pCur.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
// getting error in log cat.
Uri my_contact_Uri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_URI,
String.valueOf(id));
aPerson.setImage_Uri(my_contact_Uri);
Log.d(" Number List",
" " + aPerson.getName() + " "
+ aPerson.getNumber() + " "
+ aPerson.getImage_Uri());
allContacts.add(aPerson);
}
pCur.close();
}
}
}
return allContacts;
}
我自定义的适配器 ContactListAdapter getView()就像:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_row, null);
convertView.setMinimumHeight(50);
holder.textViewContactName = (TextView) convertView
.findViewById(R.id.textview_contact_name);
holder.textView_Contact_Number = (TextView) convertView
.findViewById(R.id.textview_number);
holder.imgViewContactImage = (ImageView) convertView
.findViewById(R.id.imgViewContactImage);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
if (allContactsNumbers.size() <= 0) {
holder.textViewContactName.setText("No Data");
} else {
holder = (ViewHolder) convertView.getTag();
String name = allContactsNumbers.get(position).getName();
holder.textViewContactName.setText(name);
String number = allContactsNumbers.get(position).getNumber();
holder.textView_Contact_Number.setText(number);
Uri Uri = allContactsNumbers.get(position).getImage_Uri();
if (Uri != null) {
holder.imgViewContactImage.setImageURI(Uri);
} else {
holder.imgViewContactImage.setImageResource(R.id.useLogo);
}
Log.d("Contacts in Adapter", "" + name + "" + number);
}
return convertView;
}
我的人员类:
public class Person {
// has setter & getter
private String name;
private String phone_number;
private Uri image_uri; }
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
要获得图像Uri试试这个:
Uri my_contact_Uri = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
如果setImageURI(my_contact_Uri)
不起作用,请尝试:
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),Uri.parse(my_contact_Uri));