0

I am trying to display contact photo on my lazy adapter. I managed to get the Photo_ID put them into an arrayList I am not sure how to display it on image view.

Here is what I done:

        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);
}

}

When log the value of photo: I get the photo_ID#. The adapter Class that I called shows the name like this:

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;

}

}

I am stuck on displaying the photo ID on the adapter side?

TheDevMan
  • 5,914
  • 12
  • 74
  • 144
  • Whats a lazy adapter and why not post adapter code? – Shark Jun 21 '13 at 15:41
  • a List View is alistview, an adapter is in charge of transforming data into views - "a customized list view" is essentially a styled XML inflated into the listview's vies; please post the adapter..... – Shark Jun 21 '13 at 15:44
  • Yep. updated the code Please check..the question Thanks! – TheDevMan Jun 21 '13 at 15:45
  • Do you have a concrete `Contact` class you wish to use or do you just get/set values from the HashMap? – Shark Jun 21 '13 at 15:52

2 Answers2

0

Try getting PHOTO_URI or PHOTO_THUMBNAIL_URI instead of PHOTO_ID. Then simply display it on an ImageView using your adapter.

Reference: ContactsContract.Contacts

hypd09
  • 1,183
  • 1
  • 15
  • 23
  • I am not able to get the PHOTO_URI, It just gives me PHOTO_ID...in the query that I am trying out – TheDevMan Jun 21 '13 at 16:03
  • [This question](http://stackoverflow.com/questions/2383580/how-do-i-load-a-contact-photo) might help you. – hypd09 Jun 21 '13 at 16:41
0

After deep research I figured out this would be the easiest way to display an image. It is working fine now!

 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);
    }
TheDevMan
  • 5,914
  • 12
  • 74
  • 144