简体   繁体   中英

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.

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)

            }
        });

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 (eg, via a USB cable).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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