在Android 6.0以后,针对系统安全性考虑,不只是在 AndroidManifest.xml中申请权限,还需要动态申请权限,如:
private void requestPermission(){
if (Build.VERSION.SDK_INT >= 23) {
int REQUEST_CODE_PERMISSION_STORAGE = 100;
String[] permissions = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CAMERA
};
for (String str : permissions) {
if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
this.requestPermissions(permissions, REQUEST_CODE_PERMISSION_STORAGE);
return;
}
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode==REQUEST_CODE_PERMISSION_STORAGE){
//权限申请成功后做相应的处理
}
}
通过这样 就可以确保申请每一个需要申请的动态权限.