解决Android 13 WRITE_EXTERNAL_STORAGE , READ_EXTERNAL_STORAGE不弹出的问题
在Android 13(API 33)之前,加入了如下代码
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
如果 compileSdk 设置 32 或者以下,那么就没什么问题。当设置33 或者以上,系统就会自动设置无权限。那在 33 及以上的怎么设置呢?
看下面的代码。
在xml中如下设置文章来源:https://www.toymoban.com/news/detail-860356.html
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
动态获取权限的代码如下:文章来源地址https://www.toymoban.com/news/detail-860356.html
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (checkSelfPermission(REQUESTED_PERMISSIONS[0], PERMISSION_REQ_ID)
&& checkSelfPermission(REQUESTED_PERMISSIONS[1], PERMISSION_REQ_ID)
&& checkSelfPermission(REQUESTED_PERMISSIONS[3], PERMISSION_REQ_ID )
&& checkSelfPermission(REQUESTED_PERMISSIONS[4], PERMISSION_REQ_ID )
&& checkSelfPermission(REQUESTED_PERMISSIONS[5], PERMISSION_REQ_ID )) {
// 获取到权限,开始你的代码
}
} else {
if (checkSelfPermission(REQUESTED_PERMISSIONS[0], PERMISSION_REQ_ID)
&& checkSelfPermission(REQUESTED_PERMISSIONS[1], PERMISSION_REQ_ID)
&& checkSelfPermission(REQUESTED_PERMISSIONS[2], PERMISSION_REQ_ID ) ) {
// 获取到权限,开始你的代码
}
}
private boolean checkSelfPermission(String permission, int requestCode) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, REQUESTED_PERMISSIONS, requestCode);
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQ_ID) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED
|| grantResults[1] != PackageManager.PERMISSION_GRANTED
|| grantResults[3] != PackageManager.PERMISSION_GRANTED
|| grantResults[4] != PackageManager.PERMISSION_GRANTED
|| grantResults[5] != PackageManager.PERMISSION_GRANTED ) {
// 未获取到权限,开始你的代码
return;
}
} else {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED
|| grantResults[1] != PackageManager.PERMISSION_GRANTED
|| grantResults[2] != PackageManager.PERMISSION_GRANTED ) {
// 未获取到权限,开始你的代码
return;
}
}
// 获取到权限,开始你的代码
}
}
到了这里,关于Android 13 WRITE_EXTERNAL_STORAGE , READ_EXTERNAL_STORAGE不弹出的问题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!