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)
}
});