5

Android provides many options for storing data persistently on the device. I have opted for internal storage, so please don't make suggestions for storing on an SD card. (I've seen too many questions about internal storage have answers for SD cards!!)

I would like to create subdirectories in my application's internal storage directory. I followed this SO answer, reproduced below.

File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
File fileWithinMyDir = new File(mydir, "myfile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.

Unfortunately, that's creating subdirectories with "app_" prefixes. So, from the example, the subdirectory looks like "app_mydir" (not ideal).

The answer to this SO question suggests that you can get rid of the "app_" prefix this way:

m_applicationDir = new File(this.getFilesDir() + "");
m_picturesDir = new File(m_applicationDir + "/mydir");

But I want to write a zip to something like /data/data/com.mypackages/files/mydir/the.zip.

So, in the end, my code looks like this:

File appDir = new File(getApplicationContext().getFilesDir() + "");
File subDir = new File(appDir + "/subDir");
File outFile = new File(subDir, "/creative.zip");

But, this is creating another error: "File does not exist" when I try this:

FileOutputStream fileStream = new FileOutputStream(outFile);

How can I (1) create a subdirectory without the "app_" prefix and (2) write a zip into it?

Also, if my first demand isn't reasonable, tell me why in the comments! I'm sure "app_" prefix has some meaning that escapes me atm.

Community
  • 1
  • 1
boo-urns
  • 10,136
  • 26
  • 71
  • 107

1 Answers1

11

Did you create the directories?

File appDir = getApplicationContext().getFilesDir();
File subDir = new File(appDir, "subDir");
if( !subDir.exists() )
    subDir.mkdir();
File outFile = new File(subDir, "creative.zip");

Note that you shouldn't use / anywhere in your app, just in case it changes. If you do want to create your own paths, use File.separator.

Grzegorz Dev
  • 3,073
  • 1
  • 18
  • 22
323go
  • 14,143
  • 6
  • 33
  • 41