1

I try a lot to create a folder in SD card in android 4.4.2 version. i try the following code

 String dirName="Test";
   File file = new File(Environment.getExternalStorageDirectory(), dirName);
        boolean status = file.mkdir();
                    if (status)
                        Toast.makeText(MainActivity.this,
                                "Directory created successfully", Toast.LENGTH_SHORT)
                                .show();
                        else
                            Toast.makeText(MainActivity.this, "Directory create failed",
                                    Toast.LENGTH_SHORT).show();

but it creates a folder in internal storage.

and I try another code is

  String path=System.getenv("SECONDARY_STORAGE");
                File file=new File(path +"/Test123");
                file.mkdir();

it creates a folder in External SDCARD in android 4.1 but not in android 4.4.2

So how can i create a folder in External sdcard in android 4.4.2?? If anyone know please help me..

Thanks in advance

  • 1
    Possible duplicate of [how to create a folder in android External Storage Directory?](http://stackoverflow.com/questions/24781213/how-to-create-a-folder-in-android-external-storage-directory) – Aloha Oct 10 '15 at 08:16
  • 'but it creates a folder in internal storage. '. Don't believe you. Please tell the value of 'file.getAbsolutePath()'. – greenapps Oct 10 '15 at 08:21
  • file.getAbsolutePath() gives the path of External SDCard but it does not create a folder. –  Oct 10 '15 at 08:31

2 Answers2

2

The documentation states that

Applications should not directly use this top-level directory, in order to avoid polluting the user's root namespace. Any files that are private to the application should be placed in a directory returned by Context.getExternalFilesDir, which the system will take care of deleting if the application is uninstalled.

where with top-level directory they mean the return value of Enviroment.getExternalStorageDirectory()

Using getExternalFilesDir you will have

 File file = new File (getExternalFilesDir(null), dirName);
 if (!file.exists()) {
   boolean status = file.mkdir();
   if (status) {
        Toast.makeText(MainActivity.this, "Directory created successfully", Toast.LENGTH_SHORT).show();
   } else {
        Toast.makeText(MainActivity.this, "Directory create failed", Toast.LENGTH_SHORT).show();
   }
 }

also you should be aware of the fact that mkdir() returns false if the directory already exists

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

This should be self-explanatory.

String folderName = "NewFolder";

        File file = new File(Environment.getExternalStorageDirectory(),
                folderName);
        if (!file.exists()) {
            file.mkdirs();
        }

Make sure you have added WRITE_EXTERNAL_STORAGE permission in your manifest.

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

Credits to Dhaval

Community
  • 1
  • 1
Aloha
  • 864
  • 18
  • 40
  • You posted the same code as OP did. And even without checking results. So what does that help? – greenapps Oct 10 '15 at 08:19
  • Already used this bit of code @greenapps. Already works. Edited to give proper credit. – Aloha Oct 10 '15 at 08:19
  • I already added this permission in mwnifest file. but it does not create a folder in android 4.4.2 –  Oct 10 '15 at 08:28