Android Get Application's 'Home' Data Directory
Solution 1
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
You can try Context.getApplicationInfo().dataDir
if you want the package's persistent data folder.
getFilesDir()
returns a subroot of this.
Solution 3
To get the path of file in application package;
ContextWrapper c = new ContextWrapper(this);
Toast.makeText(this, c.getFilesDir().getPath(), Toast.LENGTH_LONG).show();
Unpossible
Updated on July 29, 2022Comments
-
Unpossible almost 2 years
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.
-
Prizoff almost 12 yearsThis returns
/data/data/<homeAppDir>/files
, but how to get path with out this "files"? -
David Snabel-Caunt over 11 years@Prizoff Use Context.getApplicationInfo().dataDir, as per Kevin's answer below
-
Ali Imran almost 11 yearsare you searching for this :-stackoverflow.com/questions/5527764/get-application-directory
-
David about 10 yearsThe right way is: context.getApplicationContext().getFilesDir()
-
Michel over 9 yearsQuote: "Of course, never fails" is not completely true. They fixed a number of race conditions in Android 4.4 ...(see code.google.com/p/android/issues/detail?id=8886)
-
Erum over 9 years@Paul Mennega how can i find all pacakges and its cache size ?
-
Mooing Duck about 8 yearsThis is returning "/data/user/0/<homeAppDir>/files" on my Google Nexus 6, which is the wrong folder.
-
Mooing Duck about 8 yearsThis is returning "/data/user/0/<homeAppDir>" on my Google Nexus 6, which is the wrong folder.
-
TigerHix almost 8 years@MooingDuck It is because of the multi-user feature. android.stackexchange.com/questions/48393/…
-
user4401 over 5 yearsapplicationContext.filesDir.path.dropLast(5)
-
prateek over 3 years@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.