在尝试在Android中创建目录时什么都没有发生。

huangapple go评论72阅读模式
英文:

Nothing happens when I try to create directories in Android

问题

在我的应用中,我试图在外部存储目录中创建文件夹,但无法使其工作。我的代码看起来应该能正常工作(它会在创建目录之前等待存储权限)。所有的 mkdirs() 行都执行了,但什么都没有发生 - 甚至没有错误。

这是我的主要代码(在单独的线程上运行 - 我尝试在 UI 线程上运行 setupDir()

while(!(//检查权限
    ContextCompat.checkSelfPermission(instance, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
    && ContextCompat.checkSelfPermission(instance, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
)){
    try{Thread.sleep(200);}catch(Exception ignored){}
}

MigratedFunctions.setupDir();

这是我的 setupDir 方法

public static final String main_folder_name = "Forehead_Temperature_Detector";
public static void setupDir(){

    String main_folder = main_folder_name;
    String log_folder = "logs";
    String records_folder = "records";

    File mainFolder = new File(Environment.getExternalStorageDirectory(), main_folder);
    if (!mainFolder.exists()) {
        mainFolder.mkdirs();
    }

    File logFolder = new File(Environment.getExternalStorageDirectory() + "/" + main_folder, log_folder);
    if (!logFolder.exists()) {
        logFolder.mkdirs();
    }

    File recordsFolder = new File(Environment.getExternalStorageDirectory() + "/" + main_folder, records_folder);
    if (!recordsFolder.exists()) {
        recordsFolder.mkdirs();
    }
}

以下代码在用户通过设置菜单后在不同的线程上运行(requestPermissions 只是正常请求权限,确切的代码不重要 - 重要的是在第一个代码片段中的条件确实在授予权限后得到满足)

runOnUiThread(()->{
    if(!(//检查并请求权限(如果需要)
        ContextCompat.checkSelfPermission(instance, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
        && ContextCompat.checkSelfPermission(instance, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
    )){
        requestPermissions(PermissionHandler.PERMISSIONS_FOR_STORAGE_DISCOVERY,2);//请求码可以是任意值(默认为2)
    }
});
英文:

In my app, I'm trying to make folders in the external storage directory, but I can't get it to work. My code looks like it should work properly (it waits for storage permissions before creating the directories). All the mkdirs() lines run, but nothing happens - not even an error.

Here is my main code (which is run on a separate thread - I've tried running setupDir() on the UI thread)

        while(!(//checks for permissions
                ContextCompat.checkSelfPermission(instance, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                        && ContextCompat.checkSelfPermission(instance, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
        )){
            try{Thread.sleep(200);}catch(Exception ignored){}
        }

        MigratedFunctions.setupDir();

And here is my setupDir method

public static final String main_folder_name = "Forehead_Temperature_Detector";
public static void setupDir(){//https://stackoverflow.com/questions/24781213/how-to-create-a-folder-in-android-external-storage-directory

    String main_folder = main_folder_name;
    String log_folder = "logs";
    String records_folder = "records";

    File mainFolder = new File(Environment.getExternalStorageDirectory(), main_folder);
    if (!mainFolder.exists()) {
        mainFolder.mkdirs();
    }

    File logFolder = new File(Environment.getExternalStorageDirectory() + "/" + main_folder, log_folder);
    if (!logFolder.exists()) {
        logFolder.mkdirs();
    }

    File recordsFolder = new File(Environment.getExternalStorageDirectory() + "/" + main_folder, records_folder);
    if (!recordsFolder.exists()) {
        recordsFolder.mkdirs();
    }
}

The following code gets run on a different thread once the user is walked through a setup menu (requestPermissions just requests permissions normally, the exact code is not important - what is important is that the condition in the first snippet does in fact get satisfied once permissions are granted)

        runOnUiThread(()->{
            if(!(//checks and asks for permissions if necessary
                    ContextCompat.checkSelfPermission(instance, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                            && ContextCompat.checkSelfPermission(instance, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
            )){
                requestPermissions(PermissionHandler.PERMISSIONS_FOR_STORAGE_DISCOVERY,2);//the requestCode is arbitrary (default:2)

            }
        });

答案1

得分: 1

根据这篇文章,如果您使用目标 SDK 版本 29,没有遗留存储,应用程序仍然可以使用 getExternalFilesDir() 和类似目录而无需权限。但是,外部存储的其余部分似乎无法通过文件系统 API 访问。您既不能读取也不能写入。这包括其他应用程序创建的文件以及用户通过 USB 数据线放在设备上的文件。

英文:

Acording to this,if you use target sdk 29, Without legacy storage, apps still can use getExternalFilesDir() and similar directories without permissions. However, the rest of external storage appears to be inaccessible via filesystem APIs. You can neither read nor write. This includes both files created by other apps and files put on the device by the user (e.g., via a USB cable).

huangapple
  • 本文由 发表于 2020年7月29日 00:40:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63138893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定