How to get all contacts merged with given contact

I'm trying to show a list of contacts along with the photo associated to them, but I see most of the contacts without the photo although they do have a photo when I go to the phone's (SG2) default contact list. I figured that this is because some of those contacts are actually merged, and the picture comes from the (raw?) contact that is not displayed in the list, so the question is - how do I get all the contacts merged with a given contact? I tried to play with the SQLite, but I can't find any column that will uniquely identify merged contacts.

From code perspective, I started with this method: protected Bitmap getImageByContactId(final Context appContext, final String strContactId) { Bitmap bmContactImage = null;

    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(strContactId));
    Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);

    InputStream image = null;
    try {
        image = appContext.getContentResolver().openInputStream(photoUri);
        bmContactImage = BitmapFactory.decodeStream(image);

        try {
            image.close();
        } catch (IOException e ) {
            e.printStackTrace();
        }

    } catch (FileNotFoundException e1) {
        // This can be ignored because the Uri is generic and photo may not really exist
    }

    return bmContactImage;
}

but this only returned photos associated to the "original" contact that matched the original search criteria (in my case - the contact id) - not if the picture was associated to a contact linked to the original contact.

I was hoping to make something like the following work, if I could only get the (raw?) id of the contacts linked/associated/merged with the original contact, but I can't seem to find a way to find those "other" contacts...

public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
    Uri uri = Uri.parse(String.format("content://com.android.contacts/contacts/%s", id));
    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
    if (input == null) {
        return null;
    }
    return BitmapFactory.decodeStream(input);
}

Bottom line - what I'm trying to do is to show a photo of a contact, regardless whether it is from the contact matching the search criteria - or any of the contacts merged with that contacts - and any solution will be appreciated!

Thanks!

Answers


(I got here from our facebook page..)

here is how I get all contacts from my content resolver:

public void addContactsMultiple()
{
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
     if (cur.getCount() > 0) {          
           String id="";//Contact ID
           String name="";//Contact Name
           id = "";
           id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
           name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
           if(Integer.parseInt
           (cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
               //Getting the Contact Image
               if(cur.getString(cur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)) !=  null)
               {
                   imagePath = cur
                      .getString(cur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
               }
               else {imagePath="none";}//If there is no contact image
          }

       }
       cur.close();
       }

In my code you get the image URI and than put it in your Contact object, than you can use

      ImageView image = (ImageView)findViewById(R.id.image)
      image.setImageURI(Uri.parse(imagePath));

or you can use Picasso Image Loader: Picasso

and than use this code:

           Picasso.with(context)
           .load(imagePath)
           .skipMemoryCache()
           .into(image );

As You can see, I don't use BitMaps or decodeStream. You only need the uri of the photo (which directs your image to the Android gallery)

if you get several contacts from the same name you can use this code to marge them:

//(mContactList) is the ArrayList of contacts object I have created)
 HashSet<Contact> hs = new HashSet<Contact>();
        hs.addAll(mContactList);
        mContactList.clear();
        mContactList.addAll(hs);

this code will delete duplications

with a little adjustments you can get the image uri to the contact it belongs..

come back if you have more questions.


Need Your Help

General concept regarding dynamic textboxes on the fly

c# asp.net .net asp.net-mvc-3

I am working on a project which has a requirement to build "pages" on the fly. A page can consist of various controls like textboxes, checkbox etc. Currently when the user wants to add a new textbo...

Remove default user's location icon

android google-maps-android-api-2

Im developing an app using Google Maps v2 for Android and I managed to put a custom icon to the user's position but I can't remove the default one, so it overlays my custom icon like in the image: ...