?升级到11后
targetSdkVersion 30
?权限又原来的两个变为了三个
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
在原先的基础上面新增了这个全新啊
MANAGE_EXTERNAL_STORAGE
?
这个权限的目的是为了
该权限将授权读写所有共享存储内容,这也将同时包含非媒体类型的文件。但是获得这个权限的应用还是无法访问其他应用的应用专属目录 (app-specific directory),无论是外部存储还是内部存储。
但是这个MANAGE_EXTERNAL_STORAGE权限和存储的俩权限 授权其中一个就好了。。不然还会报错
所以正确做法应当是。android11申请11的权限。低于11的那么就还是按照原来的去申请
public final class PermissionActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 1024;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestPermission();
}
private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// 先判断有没有权限
if (Environment.isExternalStorageManager()) {
writeFile();
} else {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + context.getPackageName()));
startActivityForResult(intent, REQUEST_CODE);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// 先判断有没有权限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
writeFile();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
}
} else {
writeFile();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
writeFile();
} else {
ToastUtils.show("存储权限获取失败");
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()) {
writeFile();
} else {
ToastUtils.show("存储权限获取失败");
}
}
}
/**
* 模拟文件写入
*/
private void writeFile() {
ToastUtils.show("写入文件成功");
}
}
XXPermissions 提供了简单的权限申请。
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
api 'com.github.getActivity:XXPermissions:13.5'
申请这个权限就不用申请其他的了
?这个jetpck的插件帮我们完成了
val permission0 = Permission.ACCESS_FINE_LOCATION;
val permission2 = Permission.READ_PHONE_STATE;
val permission3 = Permission.MANAGE_EXTERNAL_STORAGE;
if (XXPermissions.isGranted(activity, permission0, permission2, permission3)
) {
onOver()
return
}
XXPermissions.with(activity)
// 申请单个权限
.permission(permission0)
// 申请多个权限
.permission(permission2)
.permission(permission3)
// 设置权限请求拦截器(局部设置)
//.interceptor(new PermissionInterceptor())
// 设置不触发错误检测机制(局部设置)
//.unchecked()
.request(object : OnPermissionCallback {
override fun onGranted(permissions: MutableList<String>, all: Boolean) {
}
override fun onDenied(
permissions: MutableList<String>,
never: Boolean
) {
}
})
在对应多个权限申请的时候就可以弹出如上代码
我们没有对android12
做判断。但是框架为我们做了。
参考?2022-02-24 Android R11 之后外部存储权限获取,Android R11 里将会引入一个特别的权限叫做 MANAGE_EXTERNAL_STORAGE,跟之前的版本有区别。_海月汐辰的博客-CSDN博客_manage_external_storage
Android 11 存储权限适配指南_雪舞飞影的博客-CSDN博客_android 存储权限?
GitHub - getActivity/XXPermissions: Android 权限请求框架,已适配 Android 12
|