How to create a directory in Android?

Everything in question. Here is my code:

    private void createDirectory(File currentDirectory) {
  File f = null;
  try {

   f = new File(currentDirectory.getCanonicalPath() + "/directory"
     + count);

   boolean success = f.mkdir();
   if (!success) {
    Toast.makeText(currentContext,
      f.getName() + " could not be created", 15).show();

   }
  } catch (IOException ioe) {
   Toast.makeText(currentContext,
     f.getName() + " could not be created", 15).show();
  }

  count++;
 }

I am writing a small file manager in Android, and I would like to add the ability to create a directory. There is no exception, and the success variable always returns false. Can someone tell me what is wrong, my code?

Thanks for your advice!

[EDIT]

BTW. When the phone is in development mode, does the application have write access to the SD card? I program on my phone (Acer liquid)

+5
source share
4 answers

You must add this permission:

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

By the way, not sure how you get the SDcard directory ... but it should be like this:

File sdDir = Environment.getExternalStorageDirectory();

, , SDCard /sdcard, .

+11

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

, Android, .

+2

File, File String .

, , , , , :

  • currentDirectory ,
  • currentDirectory -, ,
  • currentDirectory WRITE_EXTERNAL_STORAGE
+2

boolean success = f.mkdir();

boolean success = f.mkdirs();

( , , ).

0

All Articles