Failure Resolution: Opening the provider com.android.providers.contacts.ContactsProvider2

I am new to Android and I created the application from scratch using Android Studio and the LoginActivity template. I am targeting SDK 23 and the minimum 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 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 an 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 causes this exception?

+6
source share
2 answers

If you are testing on android M, you should ask the user for permission to read contacts at runtime as follows:

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

You are requesting this permission:

 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 import:

  import static android.Manifest.permission.READ_CONTACTS; 

and define the integer as the request identifier of the READ_CONTACTS Id identifier.

  private static final int REQUEST_READ_CONTACTS = 0; 

hope this helps.

+7
source

Api 23 has some security improvements (and that means some development bugs). I am sure that you have defined targetSdkVersion="23" . If you change it to 22, it will work fine, check it

-eight
source

All Articles