0

This is what I tried.

 private void createFolderInExternalStorage() {
    String storagePath = System.getenv("SECONDARY_STORAGE");
    Log.e("storagePath->",storagePath);
    String path = "not available";
    if (storagePath != null) {
        Log.e("Path->", "" + storagePath);
        File file = new File(storagePath.toString());
        Log.e("readable->", "" + file.canRead());
        Log.e("writable->", "" + file.canWrite());
        Log.e("executable->", "" + file.canExecute());
        dir = new File(storagePath + File.separator+etFolder.getText().toString());
        if (!dir.exists()) {
            dir.mkdirs();
            Toast.makeText(this,"Folder "+etFolder.getText().toString()+" created",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this,"Folder "+etFolder.getText().toString()+" already exists",Toast.LENGTH_SHORT).show();
        }
        path = dir.getPath();
    } else {
        Toast.makeText(this,"External Storage not available",Toast.LENGTH_SHORT).show();
    }
    tv.setText("External SDCARD path->" + path);
}

if Secondary storage is present then System.getenv("SECONDARY_STORAGE") return /storage/sdcard1 in my case but getting following:

03-21 12:02:26.827 14155-14155/com.think.teststorage E/readable->: false

03-21 12:02:26.827 14155-14155/com.think.teststorage E/writable->: false

03-21 12:02:26.828 14155-14155/com.think.teststorage E/executable->: false

Even in some devices getting the above status as true but folder creation fails.

I have added the permission:

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

Suggestions are welcome.

Rustam
  • 6,485
  • 1
  • 25
  • 25

4 Answers4

2

You can use this code to create a folder.

File dir = new File("path/of/your/folder");
try{
  if(dir.mkdir()) {
     System.out.println("Folder created");
  } else {
     System.out.println("Folder is not created");
  }
}catch(Exception e){
  e.printStackTrace();
}

Add this permission also :

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

For further reference : see this link

Let me know if this works for you! :)

Community
  • 1
  • 1
Vivek Bhardwaj
  • 530
  • 5
  • 16
1

Use the following lines:

        //Define the path you want
        String path = Environment.getExternalStoragePublicDirectory(Environment.YOUR_DIRECTORY) + File.separator + "YourFolderName";

        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }

YOUR_DIRECTORY is the directory where you want to create the folder, for example: DIRECTORY_DOCUMENTS, DIRECTORY_DOWNLOADS, DIRECTORY_PICTURES etc.

In your manifest should to add permission for write:

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

Hope it help!

Ivan Ivanov
  • 902
  • 5
  • 12
0

It is very simple and straightforward in android

`
    File mFolder = new File(Environment.getExternalStorageDirectory(), "Folder_Name");
    if (!mFolder.exists()) {
        mFolder.mkdirs();
        mFolder.setExecutable(true);
        mFolder.setReadable(true);
        mFolder.setWritable(true);
    }
`

Also include required permissions in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Deepak Sharma
  • 417
  • 3
  • 9
  • If Internal storage is present it will take that path not the secondary storage path. I want to check if secondary storage present then take that path and create a folder dynamically inside that. – Rustam Mar 21 '16 at 08:12
  • `Environment.getExternalStorageDirectory()` refers to that storage which the device manufacturer considers to be an "external storage". On most devices, this is removable media, like an SD card while on some it is a portion of on-device flash. So `Environment.getExternalStorageDirectory()` will get you the SD card directory on almost all devices and will not take path of internal storage if external storage is present. – Deepak Sharma Mar 21 '16 at 08:45
0

Just put these lines of code,

// create a File object for the parent directory
File wallpaperDirectory = new File("/sdcard/Wallpaper/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(wallpaperDirectory, filename);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);

and add require permission

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

Does not work on KitKat, external, physical sdcard ?? then use use

Environment.getExternalStorageDirectory().getPath();

Radhey
  • 2,139
  • 2
  • 24
  • 42
  • Then make it dynamically , whats the issue !!? from where you get your directory name? – Radhey Mar 21 '16 at 08:12
  • first it should check if secondary storage present or not if present then create a folder inside that. – Rustam Mar 21 '16 at 08:15