android - 如何获取 Android 联系人缩略图

标签 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);

        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)

希望它适用于您的代码。

关于android - 如何获取 Android 联系人缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11866165/

相关文章:

android - 带图像按钮的 ListView 效果不佳

java - 自定义 SimpleCursorAdapter - onClickListener 的重载 bindView 问题

java - Android Spinner 空指针

android - 如何动态更改android中的xml背景?

android - 单击时更改 ListView 项的背景颜色

android - 带有 Assets 数据库的 CursorAdapter

android - 从 Cursor 到 LinearLayout 的动态项目列表,无需使用 ListView

android - 应用程序错误 - 与服务器的连接不成功。

java - Java 中的 Gson : Attemped to deserialize a java. lang.Class。忘记注册类型适配器

java - 将按钮/编辑文本添加到相对布局(膨胀)