/**
* 请求权限
* <!--读写文件-->
* <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
* <!-- Android11额外添加 -->
* <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
* tools:ignore="ScopedStorage" />
* <p>
* android:requestLegacyExternalStorage="true"
*/
public static void requestRead2writePermission(Activity context, int requestCode) {
//运行设备>=Android 11.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//检查是否已经有权限
if (!Environment.isExternalStorageManager()) {
//跳转新页面申请权限
context.startActivityForResult(new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION), requestCode);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
ActivityCompat.requestPermissions(context, permissions, requestCode);
}
}
Android11 存储权限
于 2024-04-18 21:51:49 首次发布