3

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.

Haris Qurashi
  • 2,104
  • 1
  • 13
  • 28
ProgFroz
  • 197
  • 4
  • 17
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/129852/discussion-on-question-by-progfroz-cant-create-directory-with-mkdirs-androi). – Bhargav Rao Dec 06 '16 at 02:39

4 Answers4

5

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"
    ...
Jarda Pavlíček
  • 1,636
  • 17
  • 16
2

In the new Android update API 30 you can only write in your app local files app-specific files

File dir = new File(context.getFilesDir(), "YOUR_DIR");
dir.mkdirs();

or in the external storage of your app Android/data

final File dir = new File(myContext.getExternalFilesDir("FolderName"),"YOUR_DIR");

UPDATE

this answer provided another solution https://stackoverflow.com/a/65744517/8195076

UPDATE

another way is to grant this permission in manifest

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

like this answer https://stackoverflow.com/a/66968986/8195076

Wowo Ot
  • 1,362
  • 13
  • 21
0

Use code below

File directory = new File(Environment.getExternalStorageDirectory() + java.io.File.separator +"Directory");
if (!directory.exists())
        Toast.makeText(getActivity(), 
          (directory.mkdirs() ? "Directory has been created" : "Directory not created"),
        Toast.LENGTH_SHORT).show(); 
else
     Toast.makeText(getActivity(), "Directory exists", Toast.LENGTH_SHORT).show();

For Android API 23 (Marshmallow) and greater, we have to allow dangerous permissions otherwise our code will not work as we expected.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Haris Qurashi
  • 2,104
  • 1
  • 13
  • 28
  • I used your exact code but it still does not work.. I get the toast of "Dir not created" – ProgFroz Dec 06 '16 at 18:02
  • Is there maybe something in the gradle files what can interrupt the creation of a directory? – ProgFroz Dec 06 '16 at 18:32
  • Do you have the `WRITE_EXTERNAL_STORAGE` permission? I am not talking about permission declaration in your manifest file. I am saying do you have permission to write? – Haris Qurashi Dec 06 '16 at 18:36
  • I tried with runtime: int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE); – ProgFroz Dec 06 '16 at 18:44
  • Just go to your app settings and allow write sd card permission and rerun your application – Haris Qurashi Dec 06 '16 at 18:45
  • Oh man thank you very much, finally it created a directory. Was too simple to mind about I guess. – ProgFroz Dec 06 '16 at 18:48
  • For latest android versions we have to allow dangerous permission otherwise our code will not work as we expected – Haris Qurashi Dec 06 '16 at 18:54
0
String path = Environment.getExternalStorageDirectory().toString() + "/" +"MyDir";
File dir = new File(path);
if(!dir.exists())
{
    dir.mkdirs()
}
Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50
xbadal
  • 1,284
  • 2
  • 11
  • 24