0

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)

            }
        });
Daniel H.
  • 301
  • 2
  • 13
  • @Andy yes, permissions get granted elsewhere on a different thread. I'll edit that into the question... – Daniel H. Jul 28 '20 at 16:46
  • @Andy I've done this while trying to debug, but it always just returns false (and never an Exception of any kind) – Daniel H. Jul 28 '20 at 16:51
  • I've managed to work around this for now by adding `android:requestLegacyExternalStorage="true"` to the manifest, but for obvious reasons this is not an ideal solution – Daniel H. Jul 28 '20 at 16:56

1 Answers1

1

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).

Daniel H.
  • 301
  • 2
  • 13
Code Demon
  • 1,256
  • 1
  • 9
  • 15
  • I do in fact use target sdk 29. `getExternalFilesDir` is deprecated in this API level, but I was unaware that they actually broke the functionality in this way - for most purposes, they might as well have eliminated it entirely so we would know to make the switch immediately. – Daniel H. Jul 28 '20 at 17:33