Android11和Android9的存储权限有什么不同?
创始人
2024-08-20 10:30:16
0

Android 11 和 Android 9 存储权限的区别在于 Android 11 引入了新的 Scoped Storage API,用于更好地保护用户数据和隐私。相比之下,Android 9 仍然使用传统的存储权限模型。

以下是一个示例代码,用于在 Android 11 上请求存储许可:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    if (Environment.isExternalStorageManager()) {
        // Permission granted
    } else {
        Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
        Uri uri = Uri.fromParts("package", getPackageName(), null);
        intent.setData(uri);
        startActivityForResult(intent, REQUEST_CODE_STORAGE_PERMISSION);
    }
} else {
    // Request permission using old method for versions prior to Android 11
}

如果用户已经授予应用程序外部存储访问权限,Environment.isExternalStorageManager() 方法将返回 true

否则,我们启动一个新的活动 ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION,在此活动中提供一个 URI,其中包含应用程序的包名,以便用户可以授予权限。

还需要在 onActivityResult() 方法中处理结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_CODE_STORAGE_PERMISSION) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (Environment.isExternalStorageManager()) {
                // Permission granted
            } else {
                // Permission denied
            }
        } else {
            // Permission granted for versions prior to Android 11
        }
    }
}

相关内容

热门资讯

透视有挂!wepoker私局辅... 透视有挂!wepoker私局辅助(透视)wepoker有脚本吗,教程指南(有挂讲解)-哔哩哔哩1、下...
wepoker脚本!德州透视h... wepoker脚本!德州透视hhpoker(透视)器-切实解密真的有挂wepoker脚本!德州透视h...
pokemmo辅助工具!德普之... pokemmo辅助工具!德普之星透视辅助软件激活码(透视)挂-原来透视有挂一、德普之星透视辅助软件激...
透视有挂!wepoker买脚本... 透视有挂!wepoker买脚本靠谱吗(透视)约局吧开挂神器是真的吗,教程要领(有挂方略)-哔哩哔哩1...
sohoo辅助!hhpoker... sohoo辅助!hhpoker到底可以透视吗(透视)方法-本来推荐真的是有挂1、许多玩家不知道hhp...
透视辅助!pokemmo手机版... 透视辅助!pokemmo手机版修改器(透视)佛手大菠萝13道挂哪里,教程教材(有挂实锤)-哔哩哔哩佛...
epoker免费透视脚本!hh... epoker免费透视脚本!hhpoker真的假的(透视)app-本来曝光存在有挂1、下载好epoke...
透视开挂!wepokerplu... 透视开挂!wepokerplus万能挂(透视)wpk透视工作室,教程总结(有挂透视)-哔哩哔哩1.w...
wpk辅助插件!wepoker... wpk辅助插件!wepoker辅助透视软件(透视)神器-真是辅助真的是有挂运wepoker辅助透视软...
透视曝光!hhpoker是真的... 透视曝光!hhpoker是真的吗(透视)hhpoker辅助挂,教程练习(有挂工具)-哔哩哔哩1)hh...