4

Is it possible to create directory in directory. To create one directory simply call this:

File dir1 = getDir("dir1",Context.MODE_PRIVATE);

But how to create other directory in dir1 ?

this:

File dir2 =getDir("dir1"+"/"+"dir2",Context.MODE_PRIVATE);

throw exception:

File dirFile = java.lang.IllegalArgumentException: File app_dir1/dir2 contains a path separator

Thanks.

Streetboy
  • 4,351
  • 12
  • 56
  • 101

1 Answers1

5

The Context.getDir() appears to be an Android-unique method for abstracting out the process of creating a directory within the private storage area - it is not a generic way of making directories in general.

To make your child subdirectory, you should use normal java methods, such as

File dir2 =new File(dir1, "dir2").mkdir();

Note that the first parameter here is the File Object representing the first directory you created, not the name.

You may want to subsequently set the permissions on this directory.

warning: untested

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • @Marky changing this to the mkdirs() method is not needed - because the parent directory is known to exist, mkdir() is sufficient. – Chris Stratton Dec 06 '12 at 15:17