0

I am listing contacts with photo but i could not shown the photos of contacts. I get the photo uri as following:

            ContentResolver cr = getContentResolver();
//          Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null,null );
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
            if (cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                    name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    photo_id = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
                    Log.e("name", name);
                    Log.e("Photo_id", ""+photo_id);
                    photo = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, photo_id);
                    Log.e("URI", ""+photo);
                    image.setImageURI(photo); // Does not shown image.
                    ...
                  }
                 }

A sample uri(printed in Log):

content://com.android.contacts/contacts/52

i do not know the reason why uri is not shown the images. please help me.

M.A.Murali
  • 9,988
  • 36
  • 105
  • 182
  • did you debug it? it means taht either your UriArrayList doesnt have the required object or your photo_view is null. did you try to debug it? which object is null? – Sergey Benner Feb 13 '12 at 12:38
  • The ArrayList has Uri values. i get null pointer on Image view as mentioned in question – M.A.Murali Feb 13 '12 at 13:25
  • how do you set your photo_view in your viewholder? how do you initialize it? in getView() of your adapter perhaps? I don't see the code for it. – Sergey Benner Feb 13 '12 at 13:34
  • try this way http://stackoverflow.com/questions/4995379/android-set-contact-photo-in-a-listview-with-name-and-number – Hiren Dabhi Feb 13 '12 at 13:45
  • i clean project now working but The imageview.setImageURI does not show any image. a sample uri is "content://com.android.contacts/contacts/52/photo". – M.A.Murali Feb 13 '12 at 14:02
  • please see my edited question http://stackoverflow.com/questions/9260511/imageview-setimageuri-method-does-not-shown-image-on-android – M.A.Murali Feb 14 '12 at 09:38

1 Answers1

1

i have solved my problem using bitmap. the imageView.setImageURI is not used for me. so that is use the following code instead of setImageURI.

    private Bitmap loadContactPhoto(ContentResolver cr, int id) {
        Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
        if (input == null) {
            return null;
        }
        return BitmapFactory.decodeStream(input);

    } 

Here id is the contact id , not photo id of the contact.

M.A.Murali
  • 9,988
  • 36
  • 105
  • 182