如何获取Android联系人缩略图
我有一个如何获取Android联系人缩略图,android,listview,android-cursoradapter,Android,Listview,Android Cursoradapter,我有一个listview适配器,我正在newView方法中尝试以下操作: @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { final LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(layout, parent, false)
listview
适配器,我正在newView
方法中尝试以下操作:
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
long contactId = Long.valueOf(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)));
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
boolean hasPhone = Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
String thumbnailUri = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
TextView name_text = (TextView) v.findViewById(R.id.name_entry);
if (name_text != null) {
name_text.setText(contactName);
}
name_text.setTag(new Contact(contactId, hasPhone));
ImageView thumbnail = (ImageView) v.findViewById(R.id.thumbnail);
if (thumbnailUri != null) {
thumbnail.setImageURI(Uri.parse(thumbnailUri));
} else {
thumbnail.setImageResource(R.drawable.ic_launcher);
}
return v;
}
但当我尝试解析存储在thumbnailUri中的Uri时,我收到以下错误:
08-09 01:58:38.619: I/System.out(1471): resolveUri failed on bad bitmap uri: content://com.android.contacts/contacts/1/photo
我走错方向了吗?任何帮助都将不胜感激
private Uri getPhotoUriFromID(String id) {
try {
Cursor cur = getContentResolver()
.query(ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID
+ "="
+ id
+ " 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(id));
return Uri.withAppendedPath(person,
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
这是一个需要传递联系人id的函数,您将获得可以在imageview中轻松设置的图像的URI
使用此函数的响应uri,如imageView.setImageURI(uri)
希望它能对您的代码起作用。可能会有帮助。把它留在这里。
这样,您可以通过联系人id获取缩略图uri。
在安卓API 28上测试
ContentResolver cr = getContentResolver();
String[] projection = new String[] {ContactsContract.Contacts.PHOTO_THUMBNAIL_URI};
String where = ContactsContract.Contacts.NAME_RAW_CONTACT_ID = "?";
String[] selectionArgs = {your_contact_id}
Cursor cur = cr.query(ContactsContract.Data.CONTENT_URI, projection, where, selectionArgs, null);
String thumbnailUri;
if ((cur != null ? cur.getCount() : 0) > 0) {
if (cur.moveToNext()) {
thumbnailUri = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
}
}
if(cur!=null){
cur.close();
}
return thumbnailUri;
你能在那里放一个logcat信息,看看你试图解析的URI是什么,然后把它发布到这里吗?Log.i(“URI”,thumbnailUri);content://com.android.contacts/contacts/1/photo,我相信这是正确的。也许只是因为我在使用模拟器。我需要买一个真正的设备:/刚给自己买了一个真正的设备。这很有效,我的代码很好。尝试了这个,它返回的Uri与我已经得到的Uri相同(content://com.android.contacts/contacts/1/photo)不幸的是。我仍然收到相同的错误消息。有两个联系人id。一个是联系人id,另一个是原始联系人id,那么您为函数提供了哪个id?可能是id有问题,因此无法获取Uri。还尝试了以下方法:
String stringUri=imageUri.getPath()//works
字符串stringUri=imageUri.toString()//不工作
i.setImageURI(Uri.parse(stringUri))代码>它可能适合您。因为在我的代码中,函数工作正常。结果证明,这只是使用模拟器。很抱歉,谢谢你的帮助。如果没有你的帮助,我会一直假设我的代码是错误的:)此方法返回一个小图像,而不是一个大图像。如何检索大图像而不是小图像?