Android11scopedstorage权限未被处理。如何获取父文件夹权限并从子文件夹中获取数据?
创始人
2024-08-20 11:00:43
0

在Android 11中,访问应用程序数据和文件的方式已经发生了变化。默认情况下,应用程序只能访问其私有目录和媒体文件集合。为了访问其他目录,需要获取适当的权限。

对于scoped storage应用程序来说,获取父文件夹权限与获取普通文件夹权限并没有任何区别。只需要从适当的API获取权限。以下是获取父文件夹权限并从子文件夹中读取数据的代码示例:

if (Environment.isExternalStorageManager()) {
    // Permission is already granted
    // Get the parent folder
    File parentFolder = new File(Environment.getExternalStorageDirectory(), "myParentFolder");
    if (parentFolder.exists()) {
        // Get the child folder
        File childFolder = new File(parentFolder, "myChildFolder");
        if (childFolder.exists()) {
            // Read the file from the child folder
            File myFile = new File(childFolder, "myFile.txt");
            if (myFile.exists()) {
                // Read the contents of the file
                try {
                    BufferedReader br = new BufferedReader(new FileReader(myFile));
                    StringBuilder sb = new StringBuilder();
                    String line;
                    while ((line = br.readLine()) != null) {
                        sb.append(line);
                        sb.append('\n');
                    }
                    br.close();
                    String fileContents = sb.toString();
                    Log.d(TAG, "File contents: " + fileContents);
                } catch (IOException e) {
                    Log.e(TAG, "Error reading file", e);
                }
            }
        }
    }
} else {
    // Request permission
    Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
    startActivity(intent);
}

在这个示例中,Environment.getExternalStorageDirectory()返回外部存储设备的根目录。File类用于创建文件或目录对象。myParentFoldermyChildFolder是目录名称。这些名称可以根据需要进行更改。

相关内容

热门资讯

透视关于"wepok... 透视关于"wepoker私人局透视"一贯真的有辅助神器(哔哩哔哩)1、实时wepoker私人局透视透...
透视解密"pokem... 透视解密"pokemmo辅助脚本"果然存在有辅助攻略(哔哩哔哩)1、pokemmo辅助脚本公共底牌简...
透视开挂"wepok... 透视开挂"wepoker辅助器安装包定制"好像存在有辅助教程(哔哩哔哩)1、不需要AI权限,帮助你快...
透视有挂"wepok... 透视有挂"wepoker轻量版辅助"都是是有辅助插件(哔哩哔哩)1、进入到wepoker轻量版辅助是...
透视解迷"wepok... 透视解迷"wepoker怎么买辅助"确实真的有辅助器(哔哩哔哩)一、wepoker怎么买辅助游戏安装...
透视必备"wpk软件... 透视必备"wpk软件是正规的吗"果然存在有辅助脚本(哔哩哔哩)1、下载好wpk软件是正规的吗脚本下载...
透视透视"We po... 透视透视"We poker辅助器下载"竟然真的是有辅助脚本(哔哩哔哩)1.We poker辅助器下载...
透视必备"wepok... 透视必备"wepoker透视版下载"都是有辅助器(哔哩哔哩)运wepoker透视版下载辅助工具,进入...
透视分享"聚星ai辅... 透视分享"聚星ai辅助工具下载"好像是有辅助工具(哔哩哔哩)在进入聚星ai辅助工具下载软件靠谱后,参...
透视有挂"hhpok... 透视有挂"hhpoker辅助挂是真的吗"真是真的是有辅助神器(哔哩哔哩)1、该软件可以轻松地帮助玩家...