赞
踩
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools">
- <!-- 必须要加下面两句-->
- <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
private val readRequest = android.Manifest.permission.READ_EXTERNAL_STORAGE
- val readFlag = ActivityCompat.checkSelfPermission(this,readRequest) != PackageManager.PERMISSION_GRANTED
- // var writeFlag = ActivityCompat.checkSelfPermission(this, writeContacts) != PackageManager.PERMISSION_GRANTED
- if(readFlag){
- if(ActivityCompat.shouldShowRequestPermissionRationale(this,readRequest)){
- //引导用户打开读取联系人权限
- }
-
- ActivityCompat.requestPermissions(this,
- arrayOf(readRequest),
- 100
- )
-
- }else{
- //getContacts()
- }
- override fun onRequestPermissionsResult(
- requestCode: Int,
- permissions: Array<out String>,
- grantResults: IntArray
- ) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults)
-
- if (requestCode == 100) {
- if (grantResults.contains(PackageManager.PERMISSION_GRANTED)) {
- if (permissions.contains(readRequest)) {
- Toast.makeText(this,"成功",Toast.LENGTH_SHORT).show()
- }
-
- }
- }
- }
选择同意,之后切到后台,去系统设置进行手动用户授权
-----------------------------------------------------------------------------------------------------------------------
附上链接Android 11新特性,Scoped Storage又有了新花样_"tools:ignore=\"scopedstorage"_guolin的博客-CSDN博客
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R ||
- Environment.isExternalStorageManager()) {
- Toast.makeText(this, "已获得访问所有文件权限", Toast.LENGTH_SHORT).show()
- } else {
- val builder = AlertDialog.Builder(this)
- .setMessage("本程序需要您同意允许访问所有文件权限")
- .setPositiveButton("确定") { _, _ ->
- val intent = Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION)
- startActivity(intent)
- }
- builder.show()
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。