Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing "app_" prefix when creating subdirectories in application's internal storage

Tags:

android

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.

like image 292
user5243421 Avatar asked Jan 20 '13 07:01

user5243421


1 Answers

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.

like image 146
323go Avatar answered Nov 15 '22 11:11

323go