Android Get Application's 'Home' Data Directory

AndroidAndroid ContextHome Directory

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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

getFilesDir() returns a subroot of this.

Solution 3 - Android

To get the path of file in application package;

ContextWrapper c = new ContextWrapper(this);
Toast.makeText(this, c.getFilesDir().getPath(), Toast.LENGTH_LONG).show();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUnpossibleView Question on Stackoverflow
Solution 1 - AndroidUnpossibleView Answer on Stackoverflow
Solution 2 - AndroidLearn OpenGL ESView Answer on Stackoverflow
Solution 3 - AndroidAli ImranView Answer on Stackoverflow