1

I want to show all contacts details in a List View (like Display title name,phone Number & Image) who has a mobile phone number in my mobile contact list. I already get title name,phone Number. I follow this thread to achieve photo in the list. But not succeeded.

To get Display title name,Phone Number I use following methods. I am getting error when trying to get image with same query. Can I get all within one query or need two to get those?

public static ArrayList<Person> getAllContactsWithPhoto(Context context) {

    ArrayList<Person> allContacts = new ArrayList<Person>();
    ContentResolver cr = context.getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);

    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur
                    .getColumnIndex(ContactsContract.Contacts._ID));

            if (Integer
                    .parseInt(cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null, ContactsContract.Data.CONTACT_ID + " = ?",
                        new String[] { id }, null);

                while (pCur.moveToNext()) {

                    // fill object data 
                    Person aPerson = new Person();
                    aPerson.setName(pCur.getString(pCur
                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
                    aPerson.setNumber(pCur.getString(pCur
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));

                    // getting error in log cat.
                    Uri my_contact_Uri = Uri.withAppendedPath(
                            ContactsContract.Contacts.CONTENT_URI,
                            String.valueOf(id));

                    aPerson.setImage_Uri(my_contact_Uri);

                    Log.d(" Number List",
                            " " + aPerson.getName() + " "
                                    + aPerson.getNumber() + " "
                                    + aPerson.getImage_Uri());

                    allContacts.add(aPerson);
                }
                pCur.close();
            }
        }
    }
    return allContacts;
}

My customized Adapter ContactListAdapter getView() is like:

   @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.list_row, null);
            convertView.setMinimumHeight(50);

            holder.textViewContactName = (TextView) convertView
                    .findViewById(R.id.textview_contact_name);
            holder.textView_Contact_Number = (TextView) convertView
                    .findViewById(R.id.textview_number);
            holder.imgViewContactImage = (ImageView) convertView
                    .findViewById(R.id.imgViewContactImage);

            convertView.setTag(holder);

        } else
            holder = (ViewHolder) convertView.getTag();

        if (allContactsNumbers.size() <= 0) {
            holder.textViewContactName.setText("No Data");

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

            String name = allContactsNumbers.get(position).getName();
            holder.textViewContactName.setText(name);

            String number = allContactsNumbers.get(position).getNumber();
            holder.textView_Contact_Number.setText(number);

            Uri Uri = allContactsNumbers.get(position).getImage_Uri();
            if (Uri != null) {
                holder.imgViewContactImage.setImageURI(Uri);
            } else {
                holder.imgViewContactImage.setImageResource(R.id.useLogo);
            }

            Log.d("Contacts in Adapter", "" + name + "" + number);

        }
        return convertView;
    }

My Person Class:

  public class Person {
      // has setter & getter
    private String name;
    private String phone_number;
    private Uri image_uri; }

Somebody can help me to solve the issue?

Community
  • 1
  • 1
Shihab Uddin
  • 6,699
  • 2
  • 59
  • 74
  • http://stackoverflow.com/questions/16730064/get-android-contact-phone-number-list. check this if it helps and this http://stackoverflow.com/questions/17256932/displaying-contact-number-and-contact-name-in-a-custom-list-view/17258014#17258014 – Raghunandan Oct 19 '13 at 17:53
  • @Raghunandan: wao..i can get title name & number .but not catch image to show in list :( that post not helps me more – Shihab Uddin Oct 19 '13 at 17:55

1 Answers1

0

To get image Uri try this:

Uri my_contact_Uri = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));  

If setImageURI(my_contact_Uri) don't work try this:

Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),Uri.parse(my_contact_Uri));
ramaral
  • 6,149
  • 4
  • 34
  • 57