-1

Hey all i have a programmer thats having some trouble with getting my phone image to display in an app. the following code is what he is using to get the contact picture:

private long getContactIdFromNumber(String number) {
    Cursor c = null;
    try {
        String[] projection = new String[] { Contacts.Phones.PERSON_ID };
        Uri contactUri = Uri.withAppendedPath(
                Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number));
        c = getContentResolver().query(contactUri, projection, null, null,
                null);

        if (c.moveToFirst()) {
            long contactId = c.getLong(c
                    .getColumnIndex(Contacts.Phones.PERSON_ID));
            return contactId;
        }
    } finally {
        c.close();
    }
    return -1;
}

private boolean initPhoto() {
    boolean result = true;
    try {
        contactPhoto = People.loadContactPhoto(getApplicationContext(),
                ContentUris.withAppendedId(People.CONTENT_URI,
                        getContactIdFromNumber(phoneNumber)),
                R.drawable.header, null);
    } catch (Exception e) {
        result = false;
    }
    return result;
}

if (initPhoto()) {
   contact1.setImageBitmap(contactPhoto);
   contact2.setImageBitmap(contactPhoto);
   contact3.setImageBitmap(contactPhoto);
}

I made a contact with my phone number and image but it never loads up in the app. What should he be looking for in the code i posted above in order to make sure it is grabbing the correct phone number?

StealthRT
  • 10,108
  • 40
  • 183
  • 342

2 Answers2

0

Do you have to use Contacts instead of ContactsContract? Contacts was deprecated in API level 5.

If you're trying to load a photo for a phone number, use ContactsContract.PhoneLookup. There's an example in the reference doc for android.provider.ContactsContract.PhoneLookup. Querying on that table will return LOOKUP_KEY for the Contact. Use that to query ContactsContract.Contacts.Photo.

Joe Malin
  • 8,621
  • 1
  • 23
  • 18
  • So you mean that i would send the name of the contact to that and then it would know what image it has for that contact? – StealthRT Dec 18 '12 at 21:48
0

Turned out the number was formatted incorrectly. It was looking for xxxxxxxxxx and the number was this way (xxx)xxx-xxxx in the phone so it never matched up.

StealthRT
  • 10,108
  • 40
  • 183
  • 342