问题
I'm trying to query contact's display name:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_CODE_PICK_CONTACT:
if (resultCode == Activity.RESULT_OK) {
Uri contactUri = data.getData();
ContentResolver cr = getActivity().getContentResolver();
Cursor c = cr.query(contactUri, null, null, null, null);
if (c != null && c.moveToFirst()) {
//get the contact name
}
}
break;
}
}
Now here is the problem:
For some contacts the cursor returns empty, and I don't figure out why.
I checked the value of contactUri
, it looks like: content://com.android.contacts/data/3032
The Uri looks the same for all types of contacts I tried - facebook, google, phone etc.
For some contacts the cursor returns with a result, which is good and I can extract the name. But for others it's somehow empty even though the ContentUri is exactly the same, It was originated from Intent.getData().
Here are some facts that may have something to do with this weird problem:
All the contacts that has empty cursor are facebook contacts. the Uri looks like above.
Not all the facebook contacts cause this: I have HTC One X, which on the phonebook I can "Link" between contacts if the OS finds a relation between them (say, if it detects the similar phone number for a gmail account and facebook acount, it suggests me to "link" between them). Only the facebook contacts who were NOT "Linked" returns empty.
Right now im out of ideas. Did anyone encountered this before?
Thanks in advance.
回答1:
I had the same problem on an HTC Incredible S, which makes me think that it might be an HTC phones issue. Anyway, the workaround I ended up using is by retrieving the phone number from the bundle you get back with the data
intent.
final String phoneNumber = data.getStringExtra("android.intent.extra.PHONE_NUMBER");
at that point you'll need to do some "reverse logic" to fetch the other data of the contact using PhoneLookup.
来源:https://stackoverflow.com/questions/13823097/querying-contacts-sometimes-returns-empty-cursor