Android6.0以后动态增加权限
private void test() throws IOException { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { int hasWritePermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); if (hasWritePermission != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101); return; } }
//操作 }
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 101) { if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)// && grantResults[0] == PackageManager.PERMISSION_GRANTED) { try { test(); } catch (IOException e) { e.printStackTrace(); } } else { Toast.makeText(this, "拒绝了权限", Toast.LENGTH_SHORT).show(); } } }