Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't create directory with mkdirs() - android

I try to create a directory and use the following code:

boolean success = true;
String rootDirectory = Environment.getExternalStorageDirectory().toString();
folder = new File(rootDirectory, "Directory");
if(!(folder.exists())) {
     success = folder.mkdirs();
} else {

}

if(success) {
   Toast.makeText(getActivity().getApplicationContext(), "DIR created", Toast.LENGTH_SHORT).show();     
} else {
     Toast.makeText(getActivity().getApplicationContext(), "DIR not created successfully", Toast.LENGTH_SHORT).show();
}

I also searched for the folder if it was created, there is none.

Permissions are granted:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I also tried to ask for permission during runtime, it seems like the app has got the permission, therefore it cannot be the problem.

Some months ago I created another application and used identical code and the identical Sdk version, still it does not work with this one. I get "DIR not created successfully" and I do not know why, please help me figure out why I cannot create the directory.

like image 476
ProgFroz Avatar asked Jan 22 '26 14:01

ProgFroz


1 Answers

Asking for Manifest.permission.WRITE_EXTERNAL_STORAGE is no more sufficient for Android 10+. Now you have also enable access to legacy external storage in manifest:

<application
    android:requestLegacyExternalStorage="true"
    ...
like image 136
Jarda Pavlíček Avatar answered Jan 25 '26 20:01

Jarda Pavlíček



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!