Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2

19,605

If you are testing on android M you must ask the user for permission to read contacts at run time like this:

private void accessContacts(){
    if (!mayRequestContacts()) {
        return;
    }
    // This Build is < 6 , you can Access contacts here.
}

You ask for a permission like this:

private boolean mayRequestContacts() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return true;
    }
    if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
        return true;
    }
    if (shouldShowRequestPermissionRationale(READ_CONTACTS)) {
        Snackbar.make(mEmailView, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE)
                .setAction(android.R.string.ok, new View.OnClickListener() {
                    @Override
                    @TargetApi(Build.VERSION_CODES.M)
                    public void onClick(View v) {
                        requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
                    }
                });
    } else {
        requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
    }
    return false;
}

Then Override onRequestPermissionsResult

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    if (requestCode == REQUEST_READ_CONTACTS) {
        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission granted , Access contacts here or do whatever you need.
        }
    }
}

Add this to your imports:

      import static android.Manifest.permission.READ_CONTACTS;

and define an integer as an Id to identity READ_CONTACTS permission request.

      private static final int REQUEST_READ_CONTACTS = 0;

hope this helped.

Share:
19,605
travis1097
Author by

travis1097

Updated on June 16, 2022

Comments

  • travis1097
    travis1097 almost 2 years

    I'm new to Android and I created an app from scratch using Android Studio and the LoginActivity template. I'm targeting SDK 23 and the min version is 15. Android Studio generated the following manifest:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.uprope.uprope" >
    
        <!-- To auto-complete the email text field in the login form with the user's emails -->
        <uses-permission android:name="android.permission.GET_ACCOUNTS" />
        <uses-permission android:name="android.permission.READ_PROFILE" />
        <uses-permission android:name="android.permission.READ_CONTACTS" />
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".LoginActivity"
                android:label="@string/app_name"
                android:windowSoftInputMode="adjustResize|stateVisible" >
            </activity>
        </application>
    
    </manifest>
    

    When I try to run the empty template, I get this stack trace:

    E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
        Process: com.uprope.uprope, PID: 24790
        java.lang.RuntimeException: An error occurred while executing doInBackground()
                at android.os.AsyncTask$3.done(AsyncTask.java:309)
                at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                at java.lang.Thread.run(Thread.java:818)
         Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{85980a6 24790:com.uprope.uprope/u0a58} (pid=24790, uid=10058) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
                at android.os.Parcel.readException(Parcel.java:1599)
                at android.os.Parcel.readException(Parcel.java:1552)
                at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3550)
                at android.app.ActivityThread.acquireProvider(ActivityThread.java:4778)
                at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2018)
                at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1468)
                at android.content.ContentResolver.query(ContentResolver.java:475)
                at android.content.CursorLoader.loadInBackground(CursorLoader.java:64)
                at android.content.CursorLoader.loadInBackground(CursorLoader.java:56)
                at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
                at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
                at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:66)
                at android.os.AsyncTask$2.call(AsyncTask.java:295)
                at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                at java.lang.Thread.run(Thread.java:818)
    

    What is causing this exception?

  • electronix384128
    electronix384128 about 8 years
    This is not a solution!
  • DritanX
    DritanX about 8 years
    You need to deal with Marshmallow permission model google.com/design/spec/patterns/permissions.html
  • Al Lelopath
    Al Lelopath over 7 years
    What are mEmailView and R.string.permission_rationale?