Android 6.0 open failed: EACCES (Permission denied)

Android added new permission model for Android 6.0 (Marshmallow).

http://www.captechconsulting.com/blogs/runtime-permissions-best-practices-and-how-to-gracefully-handle-permission-removal

So you have to check Runtime Permission :

What Are Runtime Permissions?

With Android 6.0 Marshmallow, Google introduced a new permission model that allows users to better understand why an application may be requesting specific permissions. Rather than the user blindly accepting all permissions at install time, the user is now prompted to accept permissions as they become necessary during application use.

When to Implement the New Model?

it doesn’t require full support until you choose to target version 23 in your application. If you are targeting version 22 or below, your application will request all permissions at install time just as it would on any device running an OS below Marshmallow.

This information is taken from here :

Please check How to implement from this link :

http://www.captechconsulting.com/blogs/runtime-permissions-best-practices-and-how-to-gracefully-handle-permission-removal


In Android 6(Marshmallow), even though the user accepted all your permissions at install time, they can later decide to take some of those permissions away from you.

Fast solution but not recommended: maybe if you change your targetSdkVersion in the gradle to 22, the problem will be solved.

How To Implement?(Best Practices)

  1. First determine if the user’s device is a Marshmallow device or not:

    private boolean shouldAskPermission(){
    
    return(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1);
    
    }
    
  2. If shouldAskPermission() return true, ask for permission you need:

    String[] perms = {"android.permission.WRITE_EXTERNAL_STORAGE"};
    
    int permsRequestCode = 200;
    
    requestPermissions(perms, permsRequestCode);
    

The method requestPermissions(String[] permissions, int requestCode); is a public method found inside of the Android Activity class.

  1. You will receive the results of your request in the method onRequestPermissionResult as shown below:

    @Override
    public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){
    
    switch(permsRequestCode){
    
        case 200:
    
            boolean writeAccepted = grantResults[0]==PackageManager.PERMISSION_GRANTED;
    
            break;
    
    }
    
    }
    

After receiving the results, you will need to handle them appropriately.

Suggested Permissions Flow:

enter image description here

More Info:

A user with a Marshmallow device will now have the ability to revoke dangerous permissions via the application settings

Android defines some permissions as “dangerous” and some permissions as “normal.” Both are required in your application’s manifest but only dangerous permissions require a runtime request.

If you have chosen not to implement the new permissions model(runtime request), the revocation of permissions can cause unwanted user experiences and in some cases application crashes.

The table below lists all the current dangerous permissions and their respective groups:

enter image description here

If the user accepts one permission in a group/category they accept the entire group!

Source:http://www.captechconsulting.com

Using Dexter Library:

You can use Dexter. Android library that simplifies the process of requesting permissions at runtime.


You can also use ActivityCompat.requestPermissions for backwards compatible.

example:

private static final int REQUEST_CODE = 0x11;

String[] permissions = {"android.permission.WRITE_EXTERNAL_STORAGE"};
ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE); // without sdk version check

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == REQUEST_CODE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // save file
        } else {
            Toast.makeText(getApplicationContext(), "PERMISSION_DENIED", Toast.LENGTH_SHORT).show();
        }
    }
}