Home > Android Coding > Creating the Android Files Directory

Creating the Android Files Directory

November 30th, 2008 Leave a comment Go to comments

While porting some older Java code to the Android platform, I read that it was possible to place custom files in the “files” directory reserved for the application. Great, that’s exactly what I need since packaging data files with the application as resources did not make sense in this case. So how do you move your own files into this directory?

With the Android emulator running, in Eclipse open the DDMS perspective, then select the File Explorer tab. Expand the directory tree to: data/data/com.your.package. Ok so where’s the “files” directory? Good question. Other posts I read on this subject seemed to assume that it will just be there. However on my emulator the files directory is not there by default. So how are we supposed to create the files directory?

Here’s one way. In your code write a test file to the default location, meaning don’t prepend with a path. Doing this will create the files directory and will also write the test file there as well. One caveat, if you delete all of your emulator temp files, the files directory and it’s contents will also be destroyed.

Here’s some code to write a test file into the files directory:

 
try{
  // this forces the files directory to be created under the package name
  FileOutputStream fileOut = mcontext.openFileOutput("test.txt", Context.MODE_WORLD_WRITEABLE );
  fileOut.close();
}catch(Exception e){}

After executing the above code on your emulator, you should now see a directory named “files” at data/data/com.your.package/files. This code needs to import java.io.FileOutputStream and also uses a reference to the Context.

Pull and Push icons

Pull and Push icons

Move files into your new files directory by clicking the Push Files icon in the upper right corner of the DDMS-File Explorer view. If you know of an easier way to create the files directory please let me know.

Categories: Android Coding Tags: ,
  1. Anna
    December 30th, 2008 at 14:47 | #1

    I think you can also do this from the command line (with the emulator running) using push from inside the /tools directory in sdk, e.g.

    adb push ./tmp/test.txt /data/data/com.your.package/files/test.txt

    That should create the files directory.

  2. Nitin
    August 21st, 2009 at 05:30 | #2

    I tried following code but getting error “data/data/com.testpackage/files/test.txt”. Can you please help, whats wrong?

    private void CreateFile(Context mcontext){
    try{
    // this forces the files directory to be created under the package name
    FileOutputStream fileOut = mcontext.openFileOutput(“test.txt”, Context.MODE_WORLD_WRITEABLE );
    fileOut.close();
    }catch(Exception e){}
    }

  1. No trackbacks yet.
You must be logged in to post a comment.