Search code examples
androidandroid-contactsandroid-4.2-jelly-bean

Android getting contact phone image


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?


Solution

  • 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.