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();
}