简体   繁体   English

当我尝试在 Android 中创建目录时没有任何反应

[英]Nothing happens when I try to create directories in Android

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.所有mkdirs()行都运行,但没有任何反应——甚至没有错误。

Here is my main code (which is run on a separate thread - I've tried running setupDir() on the UI thread)这是我的主要代码(在单独的线程上运行 - 我尝试在 UI 线程上运行setupDir()

        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这是我的 setupDir 方法

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

        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)

            }
        });

Acording to this ,if you use target sdk 29, Without legacy storage, apps still can use getExternalFilesDir() and similar directories without permissions. 据此,如果您使用目标 sdk 29,没有旧版存储,应用程序仍然可以使用 getExternalFilesDir() 和类似目录而无需权限。 However, the rest of external storage appears to be inaccessible via filesystem APIs.但是,外部存储的 rest 似乎无法通过文件系统 API 访问。 You can neither read nor write.你既不能读也不能写。 This includes both files created by other apps and files put on the device by the user (eg, via a USB cable).这包括其他应用程序创建的文件和用户放置在设备上的文件(例如,通过 USB 电缆)。

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

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