Does someone know how do I get the path of my application directory? (e.g. /data/data/my.app.lication/
)
Currently I'm using this method: myActivity.getFilesDir().getParent();
but it seems to me like a workaround when there's a simpler solution. Also, the side-effect is the creation of the files
directory, which is un-needed.
Clarification: First - Thanks for the repliers. I try to understand if there's already exists method that does it, not for another work-around.
Based on @jared-burrows' solution. For any package, but passing Context as parameter...
public static String getDataDir(Context context) throws Exception {
return context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0)
.applicationInfo.dataDir;
}
If you're trying to get access to a file, try the openFileOutput()
and openFileInput()
methods as described here. They automatically open input/output streams to the specified file in internal memory. This allows you to bypass the directory and File
objects altogether which is a pretty clean solution.
I got this
String appPath = App.getApp().getApplicationContext().getFilesDir().getAbsolutePath();
There is a simpler way to get the application data directory with min API 4+. From any Context (e.g. Activity, Application):
getApplicationInfo().dataDir
http://developer.android.com/reference/android/content/Context.html#getApplicationInfo()
Just use this in your code
context.getApplicationInfo().dataDir
For current Android application package:
public String getDataDir(Context context) throws Exception {
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.dataDir;
}
For any package:
public String getAnyDataDir(Context context, String packageName) throws Exception {
return context.getPackageManager().getPackageInfo(packageName, 0).applicationInfo.dataDir;
}
Source: Stackoverflow.com