1

Im using this code to set a picture from a contact's phonenumber. From the phonenumber i get a contact ID, and from the ID i can get a photo URL.

Im trying to set a picture in the imageview with: mPhotoView.setImageURI(uri); But it wont work. I've debugged and the URL does NOT equal null.

The URL contains the following: content://com.android.contacts/contacts/502/photo

Does anyone know how to fix this?

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView mPhotoView = (ImageView) findViewById(R.id.imageView1);

    Uri uri = getPhotoUri(Long.parseLong(fetchContactIdFromPhoneNumber(YOUR_PHONENUMBER))); //Set a number for yourself!
    if (uri != null) {
            mPhotoView.setImageURI(uri);
    } else {
            mPhotoView.setImageResource(R.drawable.ic_launcher);
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public String fetchContactIdFromPhoneNumber(String phoneNumber) {
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(phoneNumber));
    Cursor cursor = this.getContentResolver().query(uri,
            new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID },
            null, null, null);

    String contactId = "";

    if (cursor.moveToFirst()) {
        do {
            contactId = cursor.getString(cursor
                    .getColumnIndex(PhoneLookup._ID));
        } while (cursor.moveToNext());
    }

    return contactId;
}

public Uri getPhotoUri(long contactId) {
    ContentResolver contentResolver = getContentResolver();

    try {
        Cursor cursor = contentResolver
                .query(ContactsContract.Data.CONTENT_URI,
                        null,
                        ContactsContract.Data.CONTACT_ID
                                + "="
                                + contactId
                                + " AND "

                                + ContactsContract.Data.MIMETYPE
                                + "='"
                                + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                                + "'", null, null);

        if (cursor != null) {
            if (!cursor.moveToFirst()) {
                return null; // no photo
            }
        } else {
            return null; // error in cursor process
        }

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    Uri person = ContentUris.withAppendedId(
            ContactsContract.Contacts.CONTENT_URI, contactId);
    return Uri.withAppendedPath(person,
            ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}

}

Sebastian
  • 2,698
  • 2
  • 17
  • 26
  • Check out this link. I stumbled across a few examples, worked on them and finally, could get it work. http://stackoverflow.com/questions/15860994/get-contact-photo-based-on-contact-phonenumber-android-working-example – Vamsi Challa Apr 07 '13 at 09:41

0 Answers0