列表视图中的联系人照片ID-懒惰加载



我正试图在我的懒惰适配器上显示联系人照片。我设法把Photo_ID放进了一个arrayList中,我不知道如何在图像视图中显示它。

以下是我所做的:

        while (cur.moveToNext()) 
        {
            String Sid = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String photo = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
            Log.e("Photo",""+photo);
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("name", name);
            map.put("id", Sid);
            map.put("photo",photo);
            DetailsList.add( map);
        }
    }
    cur.close(); 

    adapter = new ContactNamesAdapter(this, DataList);        
    // updating listview
    cl.setAdapter(adapter);
}

}

当记录照片的值时:我得到了照片ID。我调用的适配器类显示的名称如下:

public View getView(int position, View convertView, ViewGroup parent) 
{
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.contacts_names_row, null);
    TextView name = (TextView)vi.findViewById(R.id.name);
    name.setText(data.get(position).get("name"));   
    return vi;
}

}

我一直在适配器侧显示带照片的ID?

尝试获取PHOTO_URIPHOTO_THUMBNAIL_URI而不是PHOTO_ID。然后只需使用适配器在ImageView上显示即可。

参考:ContactsContract.Contacts

经过深入研究,我发现这将是显示图像的最简单方法。它现在工作得很好!

 ImageView profile  = (ImageView)vi.findViewById(R.id.imageHolder);                 
    Uri my_contact_Uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(id);
    InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream(getContext().getContentResolver(),my_contact_Uri);            
    if(photo_stream != null) 
    {
    BufferedInputStream buf =new BufferedInputStream(photo_stream);
    Bitmap my_btmp = BitmapFactory.decodeStream(buf);
    profile.setImageBitmap(my_btmp);
    }
    else
    {
        profile.setImageResource(R.drawable.no_pic);
    }

最新更新