2

Below is my method to get the contact list of the user and storing it in my DB, it's working in KitKat, but in android 8.0, after getting the user's permissions to read contacts, then also I am not able to read the contact list, kindly guide me for any changes required.

private void getDetails(){
        try {
            Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            ContentResolver cr = getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
            String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER };
            Cursor names = getContentResolver().query(uri, projection, null, null, null);
            int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

            names.moveToFirst();
            do {
                String name   = names.getString(indexName);
                String number = names.getString(indexNumber);

                JSONObject cust= new JSONObject();
                cust.put("name",name);
                cust.put("mobile",number);

                obj.put(cust);
            } while (names.moveToNext());
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105
  • 1
    did you getting any error ? i – Hemant Parmar Jan 23 '18 at 04:50
  • java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{bbd44a7 31747:com.s2s.doupnow/u0a123} (pid=31747, uid=10123) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS – Jimmy Gupta Jan 23 '18 at 04:54
  • you forget to add READ and WRITE permission in manifest file. or give runtime permission for read contact list. – Hemant Parmar Jan 23 '18 at 04:56
  • above marshmallow , you have to add runtime permission like [this](https://stackoverflow.com/a/36618415/5110595) add both permission in this code and check it again is running or not. – Hemant Parmar Jan 23 '18 at 05:11

0 Answers0