Pergunta

I built a contact list and I'm trying to add the contact's photos to it, I managed to get a Contact PHOTO_THUMBNAIL_DATA but I cannot figure how to load it to an ImageView.

ImageView imgView = (ImageView) itemLayout.findViewById(android.R.id.icon);
final String photoUri = cursor.getString(ContactsQuery.PHOTO_THUMBNAIL_DATA);

How can this be done?

Foi útil?

Solução

you need to use ImageView.setImageURI()

public void setImageURI (Uri uri)

Sets the content of this ImageView to the specified Uri.

This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider using setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.

for example

String uri = cursor.getString(cursor.getColumnIndex(Contacts.PHOTO_THUMBNAIL_URI));
if (uri != null) {
    Uri photoUri = Uri.parse(uri);
    iv_object_contact.setImageURI(photoUri);
}
else {
    iv_object_contact.setImageResource(res_default_contact);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top