2

I want to create directory in my android project. I have tried below code I could not find any directory in my project.

File myDir = new File(getCacheDir(), "files");
myDir.mkdir();
if(!myDir.exists())
myDir.mkdirs();
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
saiRam89
  • 365
  • 1
  • 8
  • 21

1 Answers1

1

If you just want to create a Dir in your internal storage you can do it in the below method

File mydir = context.getDir("files", Context.MODE_PRIVATE); //Creating an internal dir;
if (!mydir.exists())
{
     mydir.mkdirs();
}     

getFilesDir() returns a File object to a directory that is private to your application only.

getDir() enables you to create any file or directory in the internal memory, which is also accessible by other applications depending on the mode you create it.

getCacheDir() returns the absolute path to the application specific cache directory on the filesystem but doesn't enable you to create Dir there. You can create a file though.

Please note that this file will be stored in internal memory and can be cleared by system as well as user manually on clearing cache.

Kapil G
  • 4,081
  • 2
  • 20
  • 32