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);
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