3

I want to make dir in Sdcard, and i do follow:

  1. I added: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in manifest.
  2. I get root_path by: public static final String ROOT_PATH = Environment.getExternalStorageDirectory().toString() + "/Hello_World/"; and it returns /storage/emulated/0/Hello_World (getting when debug).

Next, I run this code:

File file = new File(Constants.ROOT_PATH);
int i = 0;
while (!file.isDirectory() && !file.mkdirs()) {
    file.mkdirs();
    Log.e("mkdirs", "" + i++);
}

I also tried both mkdirs() and mkdir() but it's showing endless loop in logcat (Log.e("mkdirs", "" + i++);). Sometimes it work, but sometimes not. Thanks for you helping!
Update: I tried my code for some devices: Nexus4, nexus7, Vega Iron, Genymotion, LG G Pro, then just Vega Iron work as expected. ??!!?!?

Justin
  • 4,400
  • 2
  • 32
  • 36
  • Ran your code to get it install in the device. Now unplug from the computer and run your app. Instead of Log.e show a toast. – Hoan Nguyen Mar 19 '14 at 03:51
  • @HoanNguyen I tried but it not helps :( – Justin Mar 19 '14 at 04:36
  • You are going to have to be more specific about the problem experienced. Edit a sample of this "endless loop in logcat" into your question. – Chris Stratton Mar 19 '14 at 04:40
  • Clear your app data using the device settings and try what I commented above again. – Hoan Nguyen Mar 19 '14 at 04:57
  • @ChrisStratton that cause by `Log.e("mkdirs", "" + i++);` command. @Hoan Nguyen: I tried clean data, reinstall app, change device (nexus4, nexus7, vega iron, genymotion) before, but it's still not working :( – Justin Mar 19 '14 at 05:46

4 Answers4

2

Try like this it will create a folder in the sd card

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/hello_world");    
myDir.mkdirs();

If you want to check that file exists or not use this code

File file = new File (myDir, file_name);
if (file.exists ()) 
   // file exist 
else 
   // file not exist  

For reference look at this answer Android saving file to external storage

Community
  • 1
  • 1
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • I wonder what is the different btw your sollution and mine? //it's still not working :( – Justin Mar 19 '14 at 04:37
  • It will create a folder in the sd card .. then what your expecting ? – RajaReddy PolamReddy Mar 19 '14 at 04:40
  • I my code, i try to create a folder named Hello_World on sdcard too, and sometime it works, sometime not :( – Justin Mar 19 '14 at 04:43
  • Both `file.isDirectory()` and `file.mkdirs()` return `false`, so my `while()` endless loop. – Justin Mar 19 '14 at 05:48
  • did you checked my code really..??? you are not creating file properly . because of that it's returning false.. File file = new File (myDir, file_name); create folder first and then check either that was a file or not. check code properly in my answer or reference link.. – RajaReddy PolamReddy Mar 19 '14 at 05:57
1

The error is cause by && in while (!file.isDirectory() && !file.mkdirs()) it should be while (!file.isDirectory() || !file.mkdirs()). You should also check if the media is mounted.

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    {
        if (DEBUG) {Log.d(TAG, "createSoundDir: media mounted");} //$NON-NLS-1$
        File externalStorage = Environment.getExternalStorageDirectory();
        if (externalStorage != null)
        {
            String externalStoragePath = externalStorage.getAbsolutePath();
            File soundPathDir = new File(externalStoragePath + File.separator + "Hello_World"); //$NON-NLS-1$

            if (soundPathDir.isDirectory() || soundPathDir.mkdirs())
            {
                String soundPath = soundPathDir.getAbsolutePath() + File.separator;
                if (DEBUG) {Log.d(TAG, "soundPath = " + soundPath);} //$NON-NLS-1$

            }
        }
    }

Cut and paste from one of my project.

Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
1

Thank all you guys, finally i found out the problem. The problem is in the while() loop, I replace by

if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && !file.isDirectory()) {
file.mkdirs();
}

Justin
  • 4,400
  • 2
  • 32
  • 36
  • Likely part of your problem was that mkdirs() only returns true when it creates the directory, and not when it already exists. – Chris Stratton Mar 19 '14 at 12:17
0

Use Environment.getExternalStorageDirectory().getAbsolutePath() as below...

public static final String ROOT_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Hello_World/";

And Check that SDCard is mounted before creating directory as below....

File file = new File(Constants.ROOT_PATH);
int i = 0;

if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
      if(!file.exists()) {
          file.mkdir();
      }
}
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
  • Thank for your answer, but it still returns `/storage/emulated/0/Hello_World` and still doesn't work. By the way, i want to know why did you edited my post (i'm get hard to read) and -1 me? Tks again! – Justin Mar 19 '14 at 03:25
  • I saw and changed, but nothing change :( – Justin Mar 19 '14 at 03:27