Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a directory in Android?

Everything is in the 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 possibility to create a directory. There is no exception and the variable success always return false. Can someone tell me what is wrong my code??

Thx for your advice !!

[EDIT]

BTW, When the phone is in Developpement mode, does an application has a write access on the sdcard? I am programming on my phone (Acer liquid)

like image 440
Dimitri Avatar asked Jan 23 '11 20:01

Dimitri


People also ask

How do you create a directory?

Use the mkdir command to create one or more directories specified by the Directory parameter. Each new directory contains the standard entries dot (.) and dot dot (..). You can specify the permissions for the new directories with the -m Mode flag.

Can you make folders on Android?

Long-press an app and drag it onto another app to create a folder. Long-press the folder to rename it. (On some devices, tap the folder to open it, then tap the name to edit it instead). You can also drag the folder into the row of favorite apps on the bottom of the Home screen on Android phones.

How do you create a file and directory?

Using mkdir and touch Commands With the combination of both mkdir and touch commands, we can accomplish the task of creating a directory and a file in a single go. Here, the parent option of mkdir helps us to create the parent directory (unless it exists) without any error, while the touch command creates a file.


2 Answers

You have to add this permission:

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

By the way, not sure how you are getting the SDcard directory... but it must be this way:

File sdDir = Environment.getExternalStorageDirectory();

It's important, because some programmers use to think that the SDCard is always /sdcard, but it could change.

like image 95
Cristian Avatar answered Nov 01 '22 08:11

Cristian


You're going to need to add

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

to your manifest file to tell Android to ask the user for your application to be allowed to.

like image 23
AaronM Avatar answered Nov 01 '22 08:11

AaronM