0

I have a app that creates two folders and stores data in these two folders.When I run this app on versions below Marshmallow everything works fine.But when I run the same app on Marshmallow the folder is not created unless I give permissions manually by going into app settings. This is the code I use to check for permissions:-

private boolean checkWritePermission(){
        int result = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (result == PackageManager.PERMISSION_GRANTED){
            Log.d("WRITE_PERMISSION","Permission granted");

            return true;

        } else {

            return false;

        }
    }

    private boolean checkReadPermission(){
        int result = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE);
        if (result == PackageManager.PERMISSION_GRANTED){

            Log.d("READ_PERMISSION","Permission granted");
            return true;

        } else {

            return false;

        }
    }

This is the code I use to request for permissions:-

private void requestReadPermission()
    {
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.READ_EXTERNAL_STORAGE)){

            Toast.makeText(context,"This permission allows you to read downloaded magazine",Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},PERMISSION_REQUEST_CODE_READ);
        }

    }

    private void requestWritePermission(){

        if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.WRITE_EXTERNAL_STORAGE)){

            Toast.makeText(context,"This permission allows you to download the magazine",Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},PERMISSION_REQUEST_CODE_WRITE);
        }
    }

    private void requestInternetPermission()
    {
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.INTERNET)){

        }else{
            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.INTERNET},PERMISSION_REQUEST_CODE_INTERNET);
        }
    }

This is code for onRequestPermissionsResult:-

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode)
    {
        case PERMISSION_REQUEST_CODE_READ:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {


            }else
            {

            }
            break;
        case PERMISSION_REQUEST_CODE_WRITE:
            if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {

            }else
            {

            }
            break;
        case PERMISSION_REQUEST_CODE_INTERNET:
            if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {

            }else
            {

            }
            break;




    }
}

And I check for permissions in oncreate as follows:-

 if (checkReadPermission() && checkWritePermission())
        {
           Log.d("ALL PERMISSIONS","All permissions granted");

        }else
        {
            requestInternetPermission();
            requestReadPermission();
            requestWritePermission();

        }
AndroidNewBee
  • 744
  • 3
  • 12
  • 36

2 Answers2

1

There are several problems with the code to check permissions:

if (checkReadPermission() && checkWritePermission()) {
   Log.d("ALL PERMISSIONS","All permissions granted");
} else {
    requestInternetPermission();
    requestReadPermission();
    requestWritePermission();
}
  1. You should ask for all permissions at once, not one by one
  2. Your first checked permission is Manifest.permission.INTERNET. It is a vulnerable permission therefore is always granted, that is why you always get permission.
  3. You can ask only one permission from the permission-group.STORAGE. That is request either: Manifest.permission.READ_EXTERNAL_STORAGE or Manifest.permission.WRITE_EXTERNAL_STORAGE

Try with this code:

if (checkWritePermission()) {
   Log.d("ALL PERMISSIONS","All permissions granted");
} else {
    requestWritePermission();
}
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
0

There's some convenient permission-checking libraries in Github.

I'm using PermissionDispatcher. Check it out.

https://github.com/hotchemi/PermissionsDispatcher

Suhyeon Lee
  • 569
  • 4
  • 18