Can anyone explain or provide a link that explains how the getFilesDir() works. I know that it returns a path on the internal storage of the specific package name. But i wanted to know how does it returns the path? Is the folder directly created when the app is installed or is it created when the method is first called? and other similar questions.
Asked
Active
Viewed 424 times
1 Answers
1
Here is the stock implementation of Context
, known as ContextImpl
.
The implementation of getFilesDir()
, as of Android 6.0, is:
@Override
public File getFilesDir() {
synchronized (mSync) {
if (mFilesDir == null) {
mFilesDir = new File(getDataDirFile(), "files");
}
return createFilesDirLocked(mFilesDir);
}
}
createFilesDirLocked()
goes through some complicated gyrations to create the directory, if needed:
// Common-path handling of app data dir creation
private static File createFilesDirLocked(File file) {
if (!file.exists()) {
if (!file.mkdirs()) {
if (file.exists()) {
// spurious failure; probably racing with another process for this app
return file;
}
Log.w(TAG, "Unable to create files subdir " + file.getPath());
return null;
}
FileUtils.setPermissions(
file.getPath(),
FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
-1, -1);
}
return file;
}
Is the folder directly created when the app is installed or is it created when the method is first called?
AFAIK, it is created when the method is called. You can test this yourself: write an app, then install it on an emulator but don't run it. Use the Android Device Monitor or adb shell
to see if the directory exists.
and other similar questions
You are welcome to peruse the Java code for Android's framework classes at your leisure.

CommonsWare
- 986,068
- 189
- 2,389
- 2,491
-
-
-
@AmarbirSingh check this link http://stackoverflow.com/questions/13006315/how-to-access-data-data-folder-in-android-device – GParekar May 07 '16 at 11:44
-
-
@CommonsWare is it possible in real device to get it through app? – Chaudhary Amar May 07 '16 at 11:50
-
if you are testing on phone it needs to be rooted. For emulator as @CommonsWare has said you can access it on emulator. For that run a app on emulator open the AndroidDeviceMonitor in the File Explorer section you can see the data/data folder – GParekar May 07 '16 at 11:53
-
@GParekar root is last option for real device anything other then this?? – Chaudhary Amar May 07 '16 at 11:58