以下均用Kotlin编写,没用过的请酌情参考,方法类似 1、首先Manifest中添加权限 这是Manifest文件的开头 这两句必须加,但是上面那句会提示这样不能在谷歌商店上架,我自己编软件自己用所以不担心,如果
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, 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()}
来源地址:https://blog.csdn.net/ncm1584716/article/details/130559957
--结束END--
本文标题: Android11 open failed: EACCES (Permission denied)的解决方法
本文链接: https://lsjlt.com/news/414276.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0