47

Im trying to build a directory called "images" on the SD card on android. This is my code but its not working? Can anyone give me some advice?

File picDirectory = new File("mnt/sdcard/images");
picDirectory.mkdirs();
Peter
  • 5,071
  • 24
  • 79
  • 115

8 Answers8

84

Update: Since Android 10,11 Storage updates, Google has restricted Storage access through standard programming language file operations.

For applications targeting only Android 10 (API 29) and above, you need to declare "requestLegacyExternalStorage="true" " in your android manifest file to use programming language based file operations.

<application android:requestLegacyExternalStorage="true" ....>

==========

You want to be sure you are correctly finding the address of your SDCard, you can't be sure its always at any particular address. You will want to do the following!

File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"images");
directory.mkdirs();

Let me know if this works for you!

You will also need the following line in your AndroidManifest.xml

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

Cronay
  • 4,875
  • 3
  • 13
  • 18
Will Tate
  • 33,439
  • 9
  • 77
  • 71
  • Hey again, Thanks for all your answers you have been very helpful and I appreciate it... So i tried your code and its the same. I'm using the File Explorer to see if the folder is created but it doesn't show that it is. I'm not sure what I'm doing wrong. – Peter Mar 12 '11 at 01:43
  • if you do a `Log.d("Peter's App", Environment.getExternalStorageDirectory()+File.separator+"images");` what do you see in `logcat`?? does it output `/mnt/sdcard/images`?? – Will Tate Mar 12 '11 at 01:50
  • It actually doesn't output anything... does that mean where ever I'm putting my code its not running? I tried putting it under a button and also under onCreate. – Peter Mar 12 '11 at 13:56
  • I tried it in onCreate(). It looks like this and it still does nothing: `public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"images"); directory.mkdirs(); Log.d("Peter's App", Environment.getExternalStorageDirectory()+File.separator+"images");` – Peter Mar 12 '11 at 14:38
  • and you never see the message in `logcat`? – Will Tate Mar 12 '11 at 14:41
  • Wait now i tried it a second time and it shows up now. I must have done something stupid wrong the first time. It now says `/mnt/sdcard/images` But when i go to the file explorere and look under `/mnt/sdcard` there is no `/images`. and i added that code under onCreate() as well – Peter Mar 12 '11 at 14:42
  • 2
    awesome! sorry it took me so long to consider permissions might be the issue ><. have fun on the rest of your project! – Will Tate Mar 12 '11 at 15:11
  • 9
    Are you kidding me, no one else would have given a question as much time as you did. Thanks again! – Peter Mar 12 '11 at 16:51
  • How would I invoke the create folder function on app install? – Si8 Jun 16 '13 at 12:56
  • Environment.getExternalStorageDirectory() is deprecated. Use this code instead of it: getContext().getExternalFilesDir(null).getAbsolutePath(). – Kuvonchbek Yakubov Nov 24 '19 at 18:01
  • I have creepy legacy code new File("/sdcard/zzz/LOG") that fails without android:requestLegacyExternalStorage now. Your answer helped to workaround (cannot easily refactor as results of the above and similar ops are used in native code I have no access to) – ror Aug 11 '20 at 09:54
13

I use this to know the result:

File yourAppDir = new File(Environment.getExternalStorageDirectory()+File.separator+"yourAppDir");

    if(!yourAppDir.exists() && !yourAppDir.isDirectory()) 
    {
        // create empty directory
        if (yourAppDir.mkdirs())
        {
            Log.i("CreateDir","App dir created");
        }
        else
        {
            Log.w("CreateDir","Unable to create app dir!");
        }
    }
    else
    {
        Log.i("CreateDir","App dir already exists");
    }
OlivierM
  • 2,820
  • 24
  • 41
Adan
  • 431
  • 1
  • 6
  • 9
4

you can use this :

File directory = new File(Environment.getExternalStorageDirectory() + "/images");
directory.mkdirs();
Nermeen
  • 15,883
  • 5
  • 59
  • 72
Mohsen Bahman
  • 1,092
  • 3
  • 15
  • 27
3

Environment.getExternalStorageDirectory() is deprecated. So you should use this:

File directory = new File(this.getExternalFilesDir(null).getAbsolutePath() + "/YourDirectoryName");
directory.mkdirs();
Kuvonchbek Yakubov
  • 558
  • 1
  • 6
  • 16
2

One thing that is worth noting is if you always get false from the mkdirs(), try to unplug your device from pc, and see if it could create folders. At least I tried, it worked for me, currently I'm looking for ways to fix this problem.

ZijunLost
  • 1,584
  • 2
  • 20
  • 25
0

To create specific root directory and its sub folder i use this code

String root = Environment.getExternalStorageDirectory().toString();//get external storage

File myDir = new File(root + "/grocery"+"/photo/technostark");//create directory and subfolder

File dir=new File(root + "/grocery"+"/data");//create subfolder

myDir.mkdirs();

dir.mkdirs();
Smittey
  • 2,475
  • 10
  • 28
  • 35
vivek pagar
  • 45
  • 1
  • 8
0

To create file inside sd card you have to use Environment.getExternalStorageDirectory()

   /**
     * Creates a new directory inside external storage if not already exist.
     *
     * @param name The directory name
     */
    public static void createNewDirectory(String name) {
            // create a directory before creating a new file inside it.
            File directory = new File(Environment.getExternalStorageDirectory(), name);
            if (!directory.exists()) {
                directory.mkdirs(); 
            }
        }

Following two important parameter which helps you to create directory 1. directory.mkdirs() :

Creates the directory named by this file, creating missing parent directories if necessary. 2. directory.mkdir() :

Creates the directory named by this file, assuming its parents exist.

For more you can how getExternalStorageDirectory() works please see link

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
0

This should help.
First get the path of the external storage:

File root=Environment.getExternalStorageDirectory();

Then:

File picDirectory = new File(root.getAbsolutePath(), "mnt/sdcard/images");
picDirectory.mkdirs();           
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68