1

I am trying to import contacts details from phonebook to my app! I can import contact details in almost all phones (Jellybean, Kitkat, Lollipop). But when i tested in Sony Experia(kitkat), the cursor contains no rows. What could be the reason? I tried lot, but couldn't solve the issue. Here is the sample code,

 Cursor phones = null;
 phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]
 { ContactsContract.CommonDataKinds.Phone.NUMBER,
  ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
  ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI
  },
  ContactsContract.CommonDataKinds.Phone.NUMBER, null, null);
Darshn
  • 1,512
  • 1
  • 23
  • 31
  • 1
    Perhaps you should use `null` for the third argument to `query()` – Karakuri Nov 04 '15 at 07:43
  • @Karakuri Thank you! that solved my issue! – Darshn Nov 04 '15 at 08:34
  • Great, I will add it as an answer. – Karakuri Nov 04 '15 at 15:26
  • @Karakuri i still wonder, how come it affected only Sony Experia device, but not others!!! :D – Darshn Nov 05 '15 at 11:40
  • If it depends on the data being formatted a certain way, perhaps the device isn't actually the problem (you would have to test it by using the same contacts data across devices). Also, OEMs sometimes make modifications to AOSP code, including the Contacts Provider, so it's possible Sony made a change that introduced this behavior. – Karakuri Nov 05 '15 at 21:18

1 Answers1

0

You can use null for the third argument to query().

The code you posted can actually cause rows to be excluded from the results, depending on how the number is stored. You are adding WHERE (data1) to the query, where data1 is a TEXT column storing the phone number. The Boolean Expressions section of the documentation on expressions explains how TEXT values are evaluated in this context.

You can verify this in a SQLite shell like so:

SQLite version 3.8.11.1 2015-07-29 20:00:57
Enter ".help" for usage hints.
sqlite> select 'PASS' where ('8005551212');
PASS
sqlite> select 'PASS' where ('800 555 1212'); -- easier to see number
PASS
sqlite> select 'PASS' where ('+18005551212'); -- E164 format
PASS
sqlite> select 'PASS' where ('(800) 555 - 1212'); -- US national format
# no results
Karakuri
  • 38,365
  • 12
  • 84
  • 104