0

In the Android versions lower than the marshmallow, i can run my application that writes a file to the External storage. And in those system the permission is granted at the time of installation of the app. But when i tried to run my application in marshmallow, it says "the app need no permissions" at the time of installation. And the app unexpectedly exits at the time i execute the write function.

Usually the device asks to grant permissions on every app at the time when it is opened for the first time. but in my app this too doesn't happens.

Zoe stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
Febin Mathew
  • 863
  • 8
  • 19

2 Answers2

2

In Android M and above, you have to ask for the permissions that are classified as "dangerous". You can find a full list of the permissions that needs to be requested here.

You can, however, avoid requesting by setting compileSDK and targetSDK to < 23. Note that this prevents you from using any of the API 23+ features.

You request the permissions like this:

ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},//Your permissions here
                1);//Random request code

Check if the user is running API 23+ by doing:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
    //Request: Use a method or add the permission asking directly into here.
}

And if you need to check the result, you can do it like this:

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {

          // If request is cancelled, the result arrays are empty.
          if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.          
            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}
Zoe stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
0

You have to provide the runtime permission handling for Android 6.0+ (Marshmallow) yourself. See here for more information.

dipdipdip
  • 1,703
  • 1
  • 15
  • 23