Reputation: 10184
I have an Address Book in an emulator with one entity in it.
I create a contact chooser for my contacts:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setData(ContactsContract.CONTENT_URI);
startActivityForResult(intent, MY_CODE);
I select the single entry in my address book and I get back an intent that has a data uri like:
content://com.android.contacts/contacts/lookup/0n354B31353539292D4553313F3F39/1/
Cool it's a CONTENT_LOOKUP_URI for ContactsContract.Contacts. I can use it immediately to get things directly accessible in ContactsContract.Contacts, like the display name. Awesome great. Ok now let's say I want to get data from ContactsContract.Contacts.Data or ContactsContract.Contacts.Photo for that user. Reading the documentation for both CONTENT_DIRECTORY entries in both ContactsContract.Contacts.Photo/Data:
This directory can be used either with a CONTENT_URI or CONTENT_LOOKUP_URI.
So I tried what's logical:
uri = Uri.withAppendedPath(uri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
Cursor c = myContentResolver.query(uri, null, null, null); // simplified
...
Now I get an error that is annoying:
java.lang.IllegalArgumentException: Unknown URL content://com.android.contacts/contacts/lookup/0n354B31353539292D4553313F3F39/1/photo
What the heck? The Contact has a display name, and yes it has a photo. Why is this considered incorrect? I have tried this on both 2.1 and 2.2 so far.
Documentation for ContactsContract.Contacts.Photo
Upvotes: 3
Views: 3382
Reputation: 6107
Use this code to get PhoneNumber of a Contact from ContactsContract.Contacts.CONTENT_URI
String contactID = null;
Cursor cursor = context.getContentResolver().query(uri,
new String[]{ContactsContract.Contacts._ID},
null, null, null);
if (cursor.moveToFirst()) {
contactID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
}
cursor.close();
// Using the contact ID now we will get contact phone number
Cursor cursorPhone = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{contactID},
null);
if (cursorPhone.moveToFirst()) {
phoneNo = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursorPhone.close();
Upvotes: 0
Reputation: 1425
Answer is late but I hope somebody can find it helpful.
For android < 3.0 it doesn't work. Use instead the _ID:
Uri contactUri;
//if at least HC uses lookupUri which is more efficient; for previous versions does not work
if (AT_LEAST_HONEYCOMB) {
contactUri = getLookupUri(contactId, contactLookupKey);
} else {
contactUri = Uri.withAppendedPath(Contacts.CONTENT_URI, "" + contactId);
}
Uri contactDetailsUri = Uri.withAppendedPath(contactUri, Contacts.Data.CONTENT_DIRECTORY);
// contactDetailsUri android<3.0: com.android.contacts/contacts/1/data
//contactDetailsUri android>=3.0: com.android.contacts/contacts/lookup/0n354B31353539292D4553313F3F39/1/data
Cursor detailsCursor = getActivity().getContentResolver().query(
contactDetailsUri,
PROJECTION,
null,
null,
SORT_ORDER);
PS: sorry for ugly code
Upvotes: 1