0

Possible Duplicate:
How do I display a contact's photo from the contact's id?

I've been trying for over a week to populate my ImageViews in my ListView with contact photos from my device, but to no avail.

Is there a COMPLETE solution as to do this for an API Level 10?

My code with LogCat:

Why are my contact photos not displaying in listview?

** CustomAdapter class:**

public class CustomAdapter extends ArrayAdapter<String> {

        Cursor c;
        String TAG = "CustomAdapter";
        private Context context = null;
        ArrayList<String> elements = null;
        private ArrayList<String> data = null;

        public static String contactName;
        public static int count = 0;

        private ArrayList<Boolean> itemChecked = null;
        public static List<String> messages;
        public static List<String> contactID;

        String body;
        String phoneNumber;

        public CustomAdapter(Context context, int type, ArrayList<String> elements) {
                super(context, type, elements);

                data = elements;
                this.elements = elements;
                this.context = context;
        }

        // THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
        static class ViewHolder {
                public ImageView photo;
                public TextView contact;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
                View rowView = convertView;
                final ViewHolder holder;

                if (rowView == null) {
                        LayoutInflater inflater = (LayoutInflater) context
                                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                        // HERE I AM INFLATING LISTVIEW LAYOUT.
                        rowView = inflater.inflate(R.layout.contact_entry, null, false);
                        holder = new ViewHolder();
                        holder.photo = (ImageView) rowView.findViewById(R.id.iv_contactPic);
                        holder.contact = (TextView) rowView
                                        .findViewById(R.id.contactEntryText);
                        rowView.setTag(holder);

                        // RETRIEVE LATEST CONTACTS WHO SENT SMS (for visual)
                        contactID = new ArrayList<String>();
                        contactID = elements;

                        String folder = "content://sms/inbox/";
                        Uri mSmsQueryUri = Uri.parse(folder);
                        contactID = new ArrayList<String>();

                        try {
                                c = context.getContentResolver().query(
                                                mSmsQueryUri,
                                                new String[] { "_id", "thread_id", "address", "date",
                                                                "body" }, null, null, null);
                                if (c == null) {
                                        Log.i(TAG, "cursor is null. uri: " + mSmsQueryUri);
                                }

                                c.moveToFirst();
                                while (c.moveToNext()) {
                                        phoneNumber = c.getString(0);
                                        contactID.add(phoneNumber);
                                }

                        } catch (Exception e) {
                                // Log.e(TAG, e.getMessage());
                        } finally {
                                c.close();
                        }

                } else {
                        holder = (ViewHolder) rowView.getTag();
                }

                if (holder != null) {

                        // bind the data to the row views
                        holder.contact.setText(data.get(position));
                        holder.photo.setImageBitmap(getByteContactPhoto(contactID
                                        .get(position)));

                // SHOW CONTACT PHOTO IF IT EXISTS. IF NOT, DEFAULT (***NOT WORKING***)
                Long l = Long.parseLong(contactID.get(position));
                        contactPhoto = loadContactPhoto(context.getContentResolver(), l);
                        if(contactPhoto == null){
                                holder.photo.setImageResource(R.drawable.ic_intel);
                        } else{
                                holder.photo.setImageBitmap(contactPhoto);
                        }

                return rowView;
        } // end if

        // GET CONTACT PHOTO
        private static Bitmap loadContactPhoto(ContentResolver cr, long  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);
}


} // end class
Community
  • 1
  • 1
  • are you able to fetch photo from contacts? – Anu Jun 18 '12 at 07:03
  • 1
    @ChocoMan : **"Is there a COMPLETE solution...?"** - So basically you want somebody to write the code for you? What have you tried? Where is it failing? What errors do you see in logcat? Post your code, and layout files (if necessary) and you might get a useful answer. – Squonk Jun 18 '12 at 07:09
  • Check out this [link][1] [1]: http://stackoverflow.com/questions/9513781/adding-the-users-contacts-photo-from-phone-to-listview-d It help you. Thanks – Md Abdul Gafur Jun 18 '12 at 07:11
  • @GAMA You everytime forget to edit TAGS of the Question,Please also do that So will not have to imporve your edits and will directly click on Approve. – MKJParekh Jun 18 '12 at 07:11
  • Please check out the [link][1] [1]: http://stackoverflow.com/questions/9513781/adding-the-users-contacts-photo-from-phone-to-listview-d It help you. Thanks – Md Abdul Gafur Jun 18 '12 at 07:13
  • @Squonk 1. No. 2. Yes (2 weeks) 3. updated –  Jun 18 '12 at 08:03
  • @ChocoMan : So in your other question that you've now linked to in your edited question here...in the `getByteContactPhoto(...)` method you call `cursor.moveToFirst();` then immediately follow it with `if (cursor.moveToNext()) {...}`. As you're only querying a single `contactId` (as passed to that method), why would you think `moveToNext()` will ever return `true`? Think about it. – Squonk Jun 18 '12 at 08:17

2 Answers2

0

Use this code for fetching the photo from contacts...........

public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
// InputStream input = ContactsContract.Contacts.Photo
if (input == null) {
    return null;
}
return BitmapFactory.decodeStream(input);
Anu
  • 552
  • 3
  • 9
0

Write this in snippet at your desired place

// set the profile picture
ImageView profile = (ImageView) findViewById(R.id.display_contact_image);
Bitmap bitmap = loadContactPhoto(getContentResolver(), _id);
if(bitmap == null) {
    //Set default contact image
    profile.setImageResource(R.drawable.default_contact_image);
} else {
    profile.setImageBitmap(bitmap);
}

Method is

private static Bitmap loadContactPhoto(ContentResolver cr, long  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);
}

And share your code which you tried yet.... (from last one week :)

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • I updated the question which contains a link to my code. From your method though, I have only one problem. It doesnt like "getContentResolver()" in my CustomAdapter class. Also, my ImageView is within that same class. –  Jun 18 '12 at 08:13
  • @ChocoMan Where is your CustomAdapter class? Do you pass activity context to your CustomAdapter? If yes then you can call getContentResolver() with context.getContentResolver(). And replace profile with holder.photo as you used in your code. – Pankaj Kumar Jun 18 '12 at 08:46
  • I've updated my question to show my adapter class. I've applied your method, but the my `contactPhoto` remains null. I do also have a context in my class too. Did I apply everything right from what you can see? –  Jun 18 '12 at 22:39