试试这个方法:
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");
}
}
这样,如果权限被授予它会来这里:
Log.v(TAG,"Permission is granted");
如果许可尚未批准,它会来这里:
Log.v(TAG,"Permission is revoked");
并试图获得许可。
如果用户授予在运行时的权限:
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
如果用户否认:
Log.v(TAG,"Permission denied");
如果版本低于Marshamallow,即低于API 23
的话,那就来这里:
Log.v(TAG,"Permission is granted <23");
来源
2017-01-23 07:18:39
OBX
入住这http://stackoverflow.com/a/31925748/2435238 – sJy
您应该遵循关于[运行时权限](https://developer.android.com/training/permissions/requesting.html)的指南。这通常是Android基础知识的良好开端 – AxelH