skip to Main Content

I have problem that contact picker code works fine on any android version below 11, but when i test it on android 11 i have this error:

2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:514)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:138)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:52)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.database.CursorWrapper.getString(CursorWrapper.java:141)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at com.tortohub.client.add.AddClientActivity.onActivityResult(AddClientActivity.java:159)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:8310)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:5008)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.os.Looper.loop(Looper.java:223)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7656)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
2021-08-05 10:15:47.617 9338-9338/com.tortohub W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

i use contact picker code from another stackoverflow question:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == RESULT_PICK_CONTACT) {
                Cursor cursor;
                try {
                    String phoneNo = "";
                    String name = "";
                    Log.i("intent",data.getData().toString());
                    Uri uri = data.getData();
                    cursor = this.getContentResolver().query(uri, null, null, null, null);
                    cursor.moveToFirst();

                    int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    phoneNo = cursor.getString(phoneIndex);

                    int nameColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                    name = cursor.getString(nameColumnIndex);

                    cursor.close();
                    Log.i("phone", phoneNo);
                    Log.i("name", name);
                    contactPicked(name,phoneNo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else {
            Log.e("MainActivity", "Failed to pick contact");
        }
    }
    private void pickContact(){
        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
        startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
    }

also i noticed that on android 11 content uri slightly changed:

android 11: intent: content://com.android.contacts/data/3
android 10: intent: content://com.android.contacts/data/1

3

Answers


  1. Chosen as BEST ANSWER

    I solved problem: the reason was that in android 11 i had to manually ask for contact read permissiom, while in android 10 that code worked fine.
    Solution: manually ask for contact read permission


  2. check the below link in android 11 you must add the permission on it

     <uses-permission android:name="android.permission.READ_PHONE_STATE"
                     android:maxSdkVersion="29" />
    <uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
    

    https://developer.android.com/about/versions/11/privacy/permissions

    Login or Signup to reply.
  3. Not just contact read permission you need to add query intent in AndroidManifest.xml

    <manifest>
        <!-- For overall Android support -->
        <uses-permission android:name="android.permission.READ_CONTACTS" />
        <application>
            ...
        </application>
        <!-- For Android 11+ support -->
        <queries>
            <intent>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.cursor.dir/contact" />
            </intent>
        </queries>
    </manifest>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search