53

A simple question, relating to the default 'home' directory when an app writes to the internal memory. By default, any files created are placed by the OS (2.2) in:

/data/data/your.package/files

When reading in files, the same default is used, when keeping in proper context via openFileInput(), openFileOutput(). But if I need to check file existence, for instance, using the File class, I need to specify the whole path in the constructor.

I see there are Environment.getDataDirectory() (returns /data), Environment.getRootDirectory() (returns /system), etc, but nothing related to getting the app's 'home' directory.

It's not a huge deal, but I'd rather not hard-code the full path into my App for File to use (say the package name changes, say the path changes in a future OS release) if there is some way to reference the app's 'home' directory programmatically.

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
Paul Mennega
  • 10,049
  • 19
  • 67
  • 109

3 Answers3

98

Of course, never fails. Found the solution about a minute after posting the above question... solution for those that may have had the same issue:

ContextWrapper.getFilesDir()

Found here.

Paul Mennega
  • 10,049
  • 19
  • 67
  • 109
  • 11
    This returns `/data/data//files`, but how to get path with out this "files"? – Prizoff Sep 19 '12 at 15:33
  • 7
    @Prizoff Use Context.getApplicationInfo().dataDir, as per [Kevin's answer below](http://stackoverflow.com/a/12792520/80425) – David Snabel-Caunt Feb 07 '13 at 12:36
  • are you searching for this :-http://stackoverflow.com/questions/5527764/get-application-directory – Ali Imran Sep 12 '13 at 07:35
  • 5
    The right way is: context.getApplicationContext().getFilesDir() – David Apr 30 '14 at 10:40
  • 2
    Quote: "Of course, never fails" is not completely true. They fixed a number of race conditions in Android 4.4 ...(see https://code.google.com/p/android/issues/detail?id=8886) – Michel Oct 20 '14 at 09:51
  • @Paul Mennega how can i find all pacakges and its cache size ? – Erum Feb 12 '15 at 04:47
  • This is returning "/data/user/0//files" on my Google Nexus 6, which is the wrong folder. – Mooing Duck Apr 06 '16 at 21:30
  • 1
    @MooingDuck It is because of the multi-user feature. http://android.stackexchange.com/questions/48393/what-kind-of-data-is-stored-in-data-user-directory – TigerHix Sep 09 '16 at 10:13
  • applicationContext.filesDir.path.dropLast(5) – user4401 Mar 16 '19 at 18:49
  • @Prizoff I believe you are looking for getFilesDir().getParentFile(). This returns the parent directory f your application's internal storage. Inside this directory you have got all other directories likes files or caches. – prateek Oct 08 '20 at 11:53
38

You can try Context.getApplicationInfo().dataDir if you want the package's persistent data folder.

getFilesDir() returns a subroot of this.

Krystian
  • 2,966
  • 2
  • 28
  • 61
Learn OpenGL ES
  • 4,341
  • 1
  • 32
  • 37
14

To get the path of file in application package;

ContextWrapper c = new ContextWrapper(this);
Toast.makeText(this, c.getFilesDir().getPath(), Toast.LENGTH_LONG).show();
Ali Imran
  • 8,249
  • 3
  • 35
  • 46