AndroidManifest.xml中出现了错误的android.permission.READ_EXTERNAL_STORAGE和android.permission.WRITE_EXTERNAL_STORAGE。
创始人
2024-10-09 00:37:51
0

1.在AndroidManifest.xml文件中添加以下权限声明:



2.在MainActivity.java或你想使用外部存储的Activity中添加以下代码:

//检查读写权限是否被授权,如果没有,则请求授权
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED
        || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE},
            REQUEST_EXTERNAL_STORAGE);
}

3.如果用户拒绝了权限请求,可以在onRequestPermissionsResult()方法中处理:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_EXTERNAL_STORAGE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //权限请求被授权
        } else {
            //权限请求被拒绝
        }
        return;
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

注意:在使用外部存储之前,必须检查是否拥有读写权限并且请求授权。同时,为了保证最佳用户体验,需要适当地处理用户拒绝权限请求的情况。

相关内容

热门资讯

透视揭幕"wepok... 透视揭幕"wepoker游戏下载"一贯是有辅助器(哔哩哔哩);1、实时wepoker游戏下载透视辅助...
透视推荐"哈糖大菠萝... 透视推荐"哈糖大菠萝开挂"切实存在有辅助脚本(哔哩哔哩)1、让任何用户在无需哈糖大菠萝开挂安装教程第...
透视教你"pokem... 透视教你"pokemmo手机版修改器"其实是真的辅助挂(哔哩哔哩)1、打开软件启动之后找到中间准星的...
透视透视"xpoke... 透视透视"xpoker怎么作必弊"真是有辅助软件(哔哩哔哩)1、xpoker怎么作必弊透视辅助软件激...
透视揭幕"hh po... 透视揭幕"hh poker辅助器先试用"真是有辅助器(哔哩哔哩)1、操作简单,无需hh poker辅...
透视专业"wepok... 透视专业"wepoker可以透视码"原来真的是有辅助教程(哔哩哔哩)1、在wepoker可以透视码插...
透视分享"poker... 透视分享"pokerrrr2辅助"都是真的是有辅助app(哔哩哔哩)1、在pokerrrr2辅助插件...
透视教你"约局吧能不... 透视教你"约局吧能不能开挂"果然是有辅助工具(哔哩哔哩)1、每一步都需要思考,不同水平的挑战会更加具...
透视透视"we po... 透视透视"we poker免费辅助器"总是有辅助教程(哔哩哔哩)1、we poker免费辅助器模拟器...
透视揭幕"wepok... 透视揭幕"wepoker能不能透视"切实存在有辅助工具(哔哩哔哩)进入游戏-大厅左侧-新手福利-激活...