Error finding Android contact

I get an error when I want to access a specific contact.

java.lang.IllegalArgumentException: invalid contact_id column

Here is a sample code:

String number = "0877777777";
                Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
                String[] projection = new String[]{ ContactsContract.PhoneLookup.CONTACT_ID };

                Cursor cur = getActivity().getContentResolver().query(uri, projection, null, null, null);

                // if other contacts have that phone as well, we simply take the first contact found.
                if (cur != null && cur.moveToNext()) {
                    Long id = cur.getLong(0);

                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(id));
                    intent.setData(contactUri);
                    startActivity(intent);

                    cur.close();
                }

Projection error, but I'm not sure how to fix it. The number is stored on the tested phone. Any advice on solving the problem would be much appreciated.

0
source share
2 answers

Just change ContactsContract.PhoneLookup.CONTACT_IDto ContactsContract.PhoneLookup._ID.

_IDin PhoneLookupjust meansCONTACT_ID

See here: https://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html

+1
source

Use ContactsContract.CommonDataKinds.Phone.CONTENT_URIinsteadContactsContract.PhoneLookup.CONTACT_ID

0

Source: https://habr.com/ru/post/1681029/


All Articles