11

我有一个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

我会以错误的方式解决这个问题吗?任何帮助将不胜感激!

4

2 回答 2

12
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)

希望它适用于您的代码。

于 2012-08-08T14:21:22.533 回答
0

可能会帮助某人。把它留在这里。
这样您就可以通过联系人 ID 获取缩略图 uri。
在 Android 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;
于 2020-11-23T14:44:59.770 回答