Try this way :
public boolean isStoragePermissionGranted()
{
if (Build.VERSION.SDK_INT >= 23)
{
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED)
{
Log.v(TAG,"Permission is granted");
return true;
}
else
{
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else
{
//permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted <23");
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]== PackageManager.PERMISSION_GRANTED)
{
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
//resume tasks needing this permission
}
else
{
Log.v(TAG,"Permission denied");
}
}
This way, If the permission is granted it would come here:
Log.v(TAG,"Permission is granted");
If permission is not already granted , it would come here:
Log.v(TAG,"Permission is revoked");
and try get the permission.
And if user grants the permission in runtime :
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
if the user denies it :
Log.v(TAG,"Permission denied");
In case the version is below Marshamallow, ie below API 23
, then it would come here:
Log.v(TAG,"Permission is granted <23");