Get Application Directory

AndroidDirectory

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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()

Solution 2 - Android

PackageManager m = getPackageManager();
String s = getPackageName();
PackageInfo p = m.getPackageInfo(s, 0);
s = p.applicationInfo.dataDir;

If eclipse worries about an uncaught NameNotFoundException, you can use:

PackageManager m = getPackageManager();
String s = getPackageName();
try {
	PackageInfo p = m.getPackageInfo(s, 0);
	s = p.applicationInfo.dataDir;
} catch (PackageManager.NameNotFoundException e) {
	Log.w("yourtag", "Error Package name not found ", e);
}

Solution 3 - Android

Just use this in your code

 context.getApplicationInfo().dataDir

Solution 4 - Android

I got this

String appPath = App.getApp().getApplicationContext().getFilesDir().getAbsolutePath();

from here:

Solution 5 - Android

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;
}

Solution 6 - Android

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.

Solution 7 - Android

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;
}

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
QuestionMByDView Question on Stackoverflow
Solution 1 - AndroidJames WaldView Answer on Stackoverflow
Solution 2 - AndroidPhilip SheardView Answer on Stackoverflow
Solution 3 - AndroidRATHIView Answer on Stackoverflow
Solution 4 - AndroiddavibqView Answer on Stackoverflow
Solution 5 - AndroidJared BurrowsView Answer on Stackoverflow
Solution 6 - AndroidChris ThompsonView Answer on Stackoverflow
Solution 7 - AndroidLeonardo CardosoView Answer on Stackoverflow