1

I want to create a new folder in external storage. I use this code:

            val folderMain = "name"

        val f = File(getExternalStorageDirectory(), folderMain)
        if (!f.exists()) {
            f.mkdirs()
        }

After executing it works and creates a new folder in internal storage, not in external storage. How can I create a new folder in external storage?

I am 100% sure I have external storage in my device and it has 14 GB space available in it and location is /storage/extSdCard.

I have tested this code on two Samsung phones Android version jellybean and Kitkat but the same result.

curveball
  • 4,320
  • 15
  • 39
  • 49
  • File file3 = new File(Environment.getExternalStorageDirectory()+"/NewFolder/"); – Mehrdad Oct 14 '18 at 09:14
  • Android uses some different terms for storage, so just to clarify: when you say external storage, do you mean the android term (shared storage), or an external SD card? – Zoe Oct 14 '18 at 09:18
  • Possible duplicate of [Create folder in Android](https://stackoverflow.com/questions/17794974/create-folder-in-android) – Radesh Oct 14 '18 at 09:25
  • Yes i want to create a new folder in sdcard.@Zoe –  Oct 14 '18 at 10:07
  • @mehrdadSs I have tried your code given but it creates a new folder in internal storage with name "NewFolder" not in external storage of my device. –  Oct 14 '18 at 10:14

3 Answers3

0

You can have the answers to your questions here :

https://developer.android.com/training/data-storage

I also recommend you to check the result of mkdirs():

if (!f.exists() && !f.mkdirs()) {
    Log.e("mDebug", "Couldn't create " + f.getName());
}
Hocine B
  • 391
  • 2
  • 8
  • Yes i have tried this code but it still creating a new folder in internal not in external sd card –  Oct 14 '18 at 10:09
0

After executing it works and creates a new folder in internal storage, not in external storage.

No, it creates the file in what the Android SDK refers to as external storage.

I am 100% sure I have external storage in my device and it has 14 GB space available in it and location is /storage/extSdCard.

That represents removable storage.

How can I create a new folder in external storage?

Your existing code creates a directory in the root of external storage. You have no ability to write to arbitrary locations on removable storage.

The simplest places to write to on removable storage are in the locations supplied by the getExternalFilesDirs(), getExternalCacheDirs(), and getExternalMediaDirs() methods on a Context. If those return 2+ locations, the second and subsequent ones are on removable storage, and you have full read/write access to them.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

try this code:

String fileName = "myfile.zip";
    File ZIP_Directory = new File("/sdcard/MOBstate/");
    ZIP_Directory.mkdirs();        
     File outputFile = new File(PDF_Directory, fileName);
     FileOutputStream fos = new FileOutputStream(outputFile);
Mehrdad
  • 1,477
  • 15
  • 17