Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace openFileOutput to save Files in subdirectory

Tags:

android

I have used many times openFileOutput.

Example:

FileOutputStream fileOutputStream = openFileOutput(fileName, Context.MODE_PRIVATE);

Now I noticed that all the files using openFileOutput method will be placed in the private app files directory.

It looks like:

/data/data/com.my.app.app/files/example.txt

How can I replace this method to store the Files on subdirectory?

Example:

/data/data/com.my.app.app/files/subdirectory/example.txt

like image 248
korunos Avatar asked Aug 12 '15 12:08

korunos


1 Answers

How can I replace this method to store the Files on subdirectory?

Replace:

FileOutputStream fileOutputStream = openFileOutput(fileName, Context.MODE_PRIVATE);

with:

File dir=new File(getFilesDir(), dirName);

dir.mkdirs();

FileOutputStream fileOutputStream = new FileOutputStream(new File(dir, fileName));

where dirName is the name of your desired subdirectory.

like image 94
CommonsWare Avatar answered Nov 08 '22 11:11

CommonsWare