0

I can see sdcard folder contents by using this

    File f = new File("/storage/extSdCard");
    if (f.exists()) {
        File[] files = f.listFiles();
        if (files != null) {
            for (File filz : files) {
                Toast.makeText(this, filz.getName() + "", Toast.LENGTH_SHORT).show();
            }
        }
    }

But when i try to create directories

File dir = new File("/storage/extSdCard/Android/Mayor");
    try {
        if (!dir.exists()) {
            if (dir.mkdirs()) {
                Toast.makeText(this, "Folder Created", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "Folder Not Created", Toast.LENGTH_LONG).show();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(this, "WTF", Toast.LENGTH_LONG).show();
    }

Its doesnt create at all. Any idea?

Meowok
  • 108
  • 1
  • 9

3 Answers3

0

// create a File object for the parent directory

File myDirectory = new File("/sdcard/Wallpaper/");

// have the object build the directory structure, if needed.

myDirectory.mkdirs();

// create a File object for the output file

File outputFile = new File(myDirectory, filename);

// now attach the OutputStream to the file object, instead of a String representation

FileOutputStream fos = new FileOutputStream(outputFile);

Note: It might be wise to use Environment.getExternalStorageDirectory() for getting the "SD Card" directory as this might change if a phone comes along which has something other than an SD Card (such as built-in flash, a'la the iPhone). Either way you should keep in mind that you need to check to make sure it's actually there as the SD Card may be removed.

UPDATE: Since API Level 4 (1.6) you'll also have to request the permission. Something like this (in the manifest) should work:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Rajesh Panchal
  • 1,140
  • 3
  • 20
  • 39
  • Still cant create directories :( File f = new File("/sdcard/Anime/"); f.mkdirs(); File outputfile = new File(f, "Demo"); try { FileOutputStream fos = new FileOutputStream(outputfile); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } – Meowok Jan 31 '17 at 05:31
  • what OS version your device do have ? – Rajesh Panchal Jan 31 '17 at 05:32
  • 5.1.1 , i already put that permission. I tried using my code but on a different path, it works but not on the sd card. I used Environment.getExternalStorageDirectory() but it points to the device storage. – Meowok Jan 31 '17 at 05:36
  • File f = new File("/" + Environment.getExternalStorageDirectory().getPath() + "/Anime/"); – Rajesh Panchal Jan 31 '17 at 05:38
  • getExternalStorageDirectory points to the device storage not on the removable sd card – Meowok Jan 31 '17 at 05:38
  • USE RUN TIME PERMITION – android_jain Jan 31 '17 at 05:58
0

File mnt = new File("/storage");

if (!mnt.exists()) mnt = new File("/mnt");

File[] roots = mnt.listFiles();

For read external sdcard, you need to mount sdcard path first then after you can able to use external sdcard path.

Jd Prajapati
  • 1,953
  • 13
  • 24
  • It is already mounted , i can even access display the name of the contents of my sd card but i just cant create a directory – Meowok Jan 31 '17 at 05:37
  • you can't get directly path of external sdcard, you need to first check extsdcard is available or not and then after you need to pick sdcard the name. – Jd Prajapati Jan 31 '17 at 05:40
  • File f = new File("/storage/extSdCard"); if (f.exists()) { File[] files = f.listFiles(); if (files != null) { for (File filz : files) { Toast.makeText(this, filz.getName() + "", Toast.LENGTH_SHORT).show(); } } } .. It displays the folders name under that path , it is from my sdcard but i just cant create directories. – Meowok Jan 31 '17 at 05:42
  • File mnt = new File("/storage"); if (!mnt.exists()) mnt = new File("/mnt"); File[] roots = mnt.listFiles(); Check result of roots... you can get the path your external sdcard, then you can use this path and create new directory. – Jd Prajapati Jan 31 '17 at 05:45
  • Results are {UsbDriveF,UsbDriveE,UsbDriveD,UsbDriveC,UsbDriveB,UsbDriveA,sdcard,extSdCard,shell} , when i used sdcard it points to device storage and when i used extSdCard it points to the removable sdcard and when i use extSdCard to create directories, it wont create a thing but when i use sdcard it creates directories in the device storage. – Meowok Jan 31 '17 at 05:57
  • extSdCard its storage, you need to get external storage name, its showing like JKLJ-JKLJ. you need to find this exact path, then after you can able to create directory. Check list files of extSdCard. – Jd Prajapati Jan 31 '17 at 06:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134444/discussion-between-jd-prajapati-and-meowok). – Jd Prajapati Jan 31 '17 at 06:03
0

You need to use DocumentFile.createDirectory(displayName) to create a directory on a removable SD card. See https://stackoverflow.com/a/35175460/1048340

Community
  • 1
  • 1
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
  • I have tried that but is there a way where i dont have to to visit another activity just to create folder? I mean , it will automatically create the folder you specified in the path for. – Meowok Feb 01 '17 at 03:01
  • You will need to request permissions for files on a removable storage. If the file was on internal storage then you need the WRITE_EXTERNAL_STORAGE permission. You only need the permission granted once. This is mandatory on Android 6.0+ (might be 5.0+, on mobile and can't check). – Jared Rummler Feb 01 '17 at 04:01