7

Referencing this question:

How to create files hierarchy in Androids '/data/data/pkg/files' directory?

What kind of a solution is there to this problem using the built in standard Java libraries, how do I navigate to the data/data/pkg directory and create folders and files in there? Maybe using some of the Android calls where possible?

When a user uses my app I wish to save any files associated with that user in a folder pkg/files/username, so that I can easily read these folders later. My other option is include the username in the filename but this isn't a very suitable and clean method in my opinion.

Community
  • 1
  • 1
Tim
  • 8,932
  • 4
  • 43
  • 64
  • I don't understand what your question is - the question you link to has the answer already. All you need to do is create the "username" directory. – RivieraKid Mar 30 '11 at 13:53

1 Answers1

18

getContext().getDir() method is your friend

File dir = ctx.getDir("my_sub_dir", Context.MODE_PRIVATE);
File newfile = new File(topDirFile.getAbsolutePath() + File.separator + "new_file_name");
newfile.createNewFile();
BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(newfile), 16 * 1024);
reflog
  • 7,587
  • 1
  • 42
  • 47
  • 1
    Once I've created the directory using that, how do I access it using openFileOutput() ? Would it not just throw an exception? – Tim Mar 30 '11 at 13:36
  • getDir() returns a File instance. from there you can get use getDir(DIR, ACCESS).getAbsolutePath() + "/" + "file" to get the actual file path and then just use FileInputStream – reflog Mar 30 '11 at 13:40
  • I get an exception whenever I try and do a getDir() for files/username, saying there is a path seperator... how am I supposed to create a directory where I want it without using a path seperator? – Tim Mar 30 '11 at 14:12