1

I try to create directory on Tablet and want to see it.

I create directory with this code

public void createDirectory(String sDirectoryName) {
  File direct = getDir(sDirectoryName, Context.MODE_PRIVATE);
  File fileWithinMyDir = new File(direct, "myfile");

  if(!direct.exist()) {
     System.out.println("Directory created");
  }
  else {
     System.out.println("Directory not created");
  }
}

I see everytime Directory created, But when I search Folder in file system, I can not see it. How can I make it visible. Am I making wrong?

EDIT:

I gave all permission on manifest. And I tried this code too

File direct = new File(Environment.getExternalStorageDirectory()+"/"+sDirectoryName);

        if(!direct.exists())
        {
             if(direct.mkdir())
             {
                 System.out.println("Directory created");
                 Toast.makeText(MainActivity.this, "Directory created", Toast.LENGTH_LONG).show();
             }
             else
             {
                 System.out.println("Directory not created");
                 Toast.makeText(MainActivity.this, "Directory not created", Toast.LENGTH_LONG).show();
             }
        }

But this is not working for me too.

EDIT: For refreshing I use this code.

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

working.

userAsk
  • 125
  • 1
  • 8

9 Answers9

0

To create a dir:

if(!direct.exist()) {
   if (direct.mkdir())
      Log.i(TAG, "Directory created");
   else
      Log.w(TAG, "Failed to create directory");
}

and don't forget permissions in your manifest file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Luca Sepe
  • 2,435
  • 1
  • 20
  • 26
0

File does not create a file if it doesn't exist. It just stores the path to it. Your if statement shows it doesn't exist.

Holloway
  • 6,412
  • 1
  • 26
  • 33
0

Try this...

public void createDirectory(String sDirectoryName)
            {    
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), sDirectoryName);
                                if (!file.exists()) {
                                    file.mkdirs();

                            }
    }
jyomin
  • 1,957
  • 2
  • 11
  • 27
0

Use below code to create directory.

public void createDirectory(String sDirectoryName) {
  File direct = getDir(sDirectoryName, Context.MODE_PRIVATE);
  File fileWithinMyDir = new File(direct, "myfile");

  if(!direct.exist()) {
     direct.mkdirs();
     System.out.println("Directory created");
  }
  else {
     System.out.println("Directory not created");
  }
}

Add permission in AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Jebasuthan
  • 5,538
  • 6
  • 35
  • 55
0

Make sure that your manifeist have the following permission

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

And in code

    File directory = new File(Environment.getExternalStorageDirectory()+DOC_FOLDER_NAME);

    // create directory if not exists 
    if(!directory.exists())
    {
        if(directory.mkdirs()) //directory is created;
            Log.i(" download ","App dir created");
        else 
            Log.w(" download ","Unable to create app dir!");
    }
vimal1083
  • 8,499
  • 6
  • 34
  • 50
0

in manifest add this:

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

and this for java file:

File myDirectory = new File(Environment.getExternalStorageDirectory(), "dirName");

if(!myDirectory.exists()) {                                 
  myDirectory.mkdirs();
}

to delete it:

 myDirectory.delete();

and this for File object for the parent directory:

 //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);
Aduait Pokhriyal
  • 1,529
  • 14
  • 30
Android
  • 8,995
  • 9
  • 67
  • 108
0

Note: because Android uses the MTP protocol for USB connections sometimes a file or folder just wont show because everything is cached and may need a refresh.

More info: Nexus 4 not showing files via MTP

Community
  • 1
  • 1
PieterAelse
  • 3,578
  • 2
  • 23
  • 33
0

Your print statement is confusing :

if(!direct.exist()) { // If directory does not exist
     System.out.println("Directory created"); // Directory created not true
}

As just creating a File object it will not create directory the code should be:

if(!direct.exist()) { // If directory does not exist
     direct.mkdir(); // Create directory
     System.out.println("Directory created");
}
else {
     System.out.println("Directory not created");
}

Also make sure to add android.permission.WRITE_EXTERNAL_STORAGE permission in your application.

Additionally its suggested not to use System.out.println in Android as On the emulator and most devices System.out.println gets redirected to LogCat and printed using Log.i(). This may not be true on very old or custom Android versions.

Community
  • 1
  • 1
Aduait Pokhriyal
  • 1,529
  • 14
  • 30
0

Is the issue not on the line File direct = getDir(sDirectoryName, Context.MODE_PRIVATE);

According to the documentation context.MODE_PRIVATE will only be visible within the app itself another program or user ID won't be able to find it.

try:

File direct = getDir(sDirectoryName, Context.MODE_WORLD_READABLE);

or

File direct = getDir(sDirectoryName, Context.MODE_WORLD_WRITEABLE);

http://developer.android.com/reference/android/content/Context.html