querying contacts - SOMETIMES returns empty cursor

自古美人都是妖i 提交于 2019-12-23 12:16:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!