Contact request - SOMETIMES returns an empty cursor

I am trying to request the display name of the contact:

@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 do not understand why. I checked the contactUri value, it looks like this: content: //com.android.contacts/data/3032

Uri looks the same for all types of contacts I tried - facebook, google, phone, etc.

For some contacts, the cursor returns with the result, which is good, and I can extract the name. But for others, it is somehow empty, although ContentUri is exactly the same, it was created from Intent.getData ().

Here are some facts that may have something to do with this strange problem:

  • All contacts that have a blank cursor are facebook contacts. Uri looks taller.

  • Not all facebook contacts cause this: I have HTC One X, which in the phone book I can “bind” between contacts if the OS detects a connection between them (say, if it detects a similar phone number for a gmail account and facebook account, it invites me to “bind” between them). Only facebook contacts that have not been linked are returned blank.

Right now out of ideas. Has anyone come across this before?

Thanks in advance.

+4
source share
1 answer

I had the same issue on HTC Incredible S, which makes me think that this could be a problem with HTC phones. Anyway, the workaround I used is to get the phone number from the set, which you will return with the intention of data .

 final String phoneNumber = data.getStringExtra("android.intent.extra.PHONE_NUMBER"); 

at this point you will need to do some “reverse logic” to get other contact data using PhoneLookup .

+1
source

All Articles