Lemon Android
Lemon Android

Reputation: 51

Android java.lang.SecurityException occured

I am new in Android. And I am Working On Dialer Through Launch Application. And I hide my Application Icon From the Menu. I successfully open the Application from Dialer In jellybean And Kitkat Device but when i open Application in Lollipop and Marshmallow System getting me Exception like,

E/GooglePlusContactsSync: Failed to clear out contacts
                                                       java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{ff7ceee 26321:com.google.android.apps.plus/u0a89} (pid=26321, uid=10089) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
                                                           at android.os.Parcel.readException(Parcel.java:1620)
                                                           at android.os.Parcel.readException(Parcel.java:1573)
                                                           at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3594)
                                                           at android.app.ActivityThread.acquireProvider(ActivityThread.java:4799)
                                                           at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2018)
                                                           at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1466)
                                                           at android.content.ContentResolver.query(ContentResolver.java:475)
                                                           at android.content.ContentResolver.query(ContentResolver.java:434)
                                                           at dit.a(PG:440)
                                                           at dit.b(PG:1388)
                                                           at diu.run(PG:325)
                                                           at java.lang.Thread.run(Thread.java:818)

E/DatabaseUtils: Writing exception to parcel
                                            java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/fs_id from pid=2208, uid=10089 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
                                                at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:605)
                                                at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:480)
                                                at android.content.ContentProvider$Transport.query(ContentProvider.java:211)
                                                at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
                                                at android.os.Binder.execTransact(Binder.java:453)

And then Application automatic close.

Upvotes: 1

Views: 5292

Answers (2)

Sandro Machado
Sandro Machado

Reputation: 10205

The Android 6.0 (Marshmallow) introduced a new way to deal with permissions on Android. Read more about it here

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app's functionality; for example, a user could choose to give a camera app access to the camera but not to the device location. The user can revoke the permissions at any time, by going to the app's Settings screen.

You need to declare your permissions at the manifest. Check for the current state:

// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.WRITE_CALENDAR);

And request them if you need:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

To deal with runtime permission in Android I usually use this library. It really helps and reduce the code boilerplate.

Upvotes: 2

Logic
Logic

Reputation: 2258

From Android 6.0 we need to check if the permission is already granted or not, so in order to read contacts you need to first check if read_contacts permission is already granted or not.

Try :

    if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.CAMERA}, 101);
        return;
    } else {
        // permission already granted
    }

and Listener after granting the permission

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case 101:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permission granted
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    }

Upvotes: 0

Related Questions