92

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.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • @MByD: Out of curiosity, what specifically are you trying to do? You should have no need for that directory. For example, if you want to work with the database directory, use `getDatabasePath()`. Or, if you want your own directory independent of anything else, use `getDir()`. – CommonsWare Apr 03 '11 at 11:56
  • @CommonsWare - I have a shared object which is not under my control that creates several folders under my package folder, and I need to access them. I just wonder why isn't the base folder available by some method. – MByD Apr 03 '11 at 12:06
  • 1
    @MyByD: Tell whoever wrote that shared object to have their head examined. – CommonsWare Apr 03 '11 at 12:51
  • 1
    @CommonsWare - What's so wrong about that? Also, My parents called me MByD for a reason, please don't call me MyByD `;)` – MByD Apr 03 '11 at 12:58
  • 2
    @MByD: Sorry about the name typo. With regard to the shared object, if they are expecting you to manipulate those files directly, at minimum they need to be giving you methods or something to get those directories, rather than you having to hack your own solution. Conversely, if they are *not* expecting you to manipulate those files directly, then they need some API to let you do what needs doing through their code. If what you're trying to do is well outside the shared object's scope, then you perhaps should not be trying to access the files. – CommonsWare Apr 03 '11 at 13:13
  • It's a very very common thing when you use the NDK – Lothar Dec 17 '18 at 20:40

7 Answers7

118

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

James Wald
  • 13,626
  • 5
  • 52
  • 63
93
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);
}
user1767754
  • 23,311
  • 18
  • 141
  • 164
Philip Sheard
  • 5,789
  • 5
  • 27
  • 42
25

Just use this in your code

 context.getApplicationInfo().dataDir
RATHI
  • 5,129
  • 8
  • 39
  • 48
21

I got this

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

from here:

Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
davibq
  • 1,089
  • 11
  • 31
  • 2
    getFilesDir returns a subdir of dataDir – njzk2 Aug 22 '13 at 13:48
  • 10
    Please take time to read the question you are answering. The getFilesDir() mechanism was already proposed and rejected as undesirable in the text of the question itself. Additionally, as pointed out by njzk2 it does not return the desired result unless you take its parent directory, as done in the question. – Chris Stratton Mar 07 '14 at 21:08
10

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;
}
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
3

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;
}
Leonardo Cardoso
  • 1,164
  • 2
  • 12
  • 22
3

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.

Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
  • Thanks, but actually I'm just trying to understand if there is a function that returns my application dir path, I'm using it a lot and my current solution looks pretty ugly to me. – MByD Apr 03 '11 at 05:31
  • @MByd, ah. I gotcha, sorry it wasn't what you were looking for! – Chris Thompson Apr 03 '11 at 05:36
  • @MByD: When you say "I'm using it a lot", what do you mean? You do it in a lot of places in your code??? – Squonk Apr 03 '11 at 05:49
  • @MisterSquank - Yes. It seems like it's going to move to util class, but still... – MByD Apr 03 '11 at 05:52
  • As with the getFilesDir() approaches, this will not access the desired path but rather a subdirectory within it. – Chris Stratton Mar 07 '14 at 21:11