简体   繁体   English

如何管理 Android 11 中的文件和文件夹保持向后兼容性

[英]How to manage files and folder in Android 11 maintaining backwards-compatibility

I need to make my application compatible with android versions greater than 29 (Android 11+).我需要使我的应用程序与大于 29(Android 11+)的 android 版本兼容。 Currently I create a folder tree with the following code:目前我使用以下代码创建一个文件夹树:

   public void createFolder() {
        try {
            ArrayList<File> listDir = new ArrayList<>();
            listDir.add(new File(MAIN_FOLDER));
            listDir.add(new File(CONFIG_FOLDER_ROOT));
            listDir.add(new File(PDF_FOLDER));
            listDir.add(new File(THUMB_FOLDER));
            listDir.add(new File(LOGS_FOLDER));
            listDir.add(new File(ARCHIVE_FOLDER));
            listDir.add(new File(ZOMBIE_FOLDER));
            listDir.add(new File(DATASHEET_FOLDER));

            for (int i = 0; i < listDir.size(); i++) {
                if (!listDir.get(i).exists()) {
                    if (listDir.get(i).mkdirs()) {
                        //Creo il file .incarichi
                        if (listDir.get(i).getName().compareToIgnoreCase(".Data") == 0) {
                            File fileIncarichi = new File(listDir.get(i).getAbsolutePath(), ASSIGNMENT_FILE);
                            if (!fileIncarichi.exists()) {
                                fileIncarichi.createNewFile();
                            }
                        }

                        //Creo il file .warning
                        if (listDir.get(i).getName().compareToIgnoreCase(".Logs") == 0) {
                            File fileIncarichi = new File(listDir.get(i).getAbsolutePath(), WARNING_FILE);
                            if (!fileIncarichi.exists()) {
                                fileIncarichi.createNewFile();
                            }
                            File fileConfig = new File(listDir.get(i).getAbsolutePath(), CONFIG_FILE);
                            if (!fileConfig.exists()) {
                                fileConfig.createNewFile();
                            }
                        }

                        //Creo i file .nomedia
                        File fileMedia = new File(listDir.get(i).getAbsolutePath(), ".nomedia");
                        if (!fileMedia.exists()) {
                            fileMedia.createNewFile();
                        }
                    }
                }
            }
        } catch (Exception e) {
            wil.WriteFile("1)FilesUtilities - Exception: " + e.getMessage());
        }
    }

These are examples of the variables I pass to the "newFile()" functions:这些是我传递给“newFile()”函数的变量示例:

public final static String MAIN_FOLDER = Environment.getExternalStorageDirectory() + "/.MYAPP";
public final static String THUMB_FOLDER = Environment.getExternalStorageDirectory() + "/.MYAPP/.Thumbs";

To maintain compatibility with new android versions I have inserted in the manifest and right now everything is working fine:为了保持与新的 android 版本的兼容性,我已在清单中插入,现在一切正常:

android:requestLegacyExternalStorage="true"

I received a notice via the play store that by May 5 I have to adapt my app so that it is compliant to the new policies, so I started to read up on how to make the app compatible with all versions of android, but I have some doubts on how to proceed.我通过 Play 商店收到通知,到 5 月 5 日我必须调整我的应用程序以使其符合新政策,所以我开始阅读如何使应用程序与 android 的所有版本兼容,但我有关于如何进行的一些疑问。

Keeping in mind that I cannot create "custom" folders, could it be enough to create my directories directly in a public folder such as "Download, Documents" or is it necessary to completely change the procedures for creating and accessing files using "media store" or the "store access framework"?请记住,我无法创建“自定义”文件夹,是否可以直接在公共文件夹(例如“下载,文档”)中创建我的目录,或者是否有必要完全更改使用“媒体存储”创建和访问文件的程序”或“商店访问框架”?

Write your files in the classic way to a subdirectory of the public Documents folder.以经典方式将文件写入公共 Documents 文件夹的子目录。

They will survive.他们会活下来。

But not reachable by classic file means (unless your app has 'all files access') but with SAF.但无法通过经典文件方式访问(除非您的应用程序具有“所有文件访问权限”)但使用 SAF。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM