Get application catalog

Does anyone know how to get the path to the application directory? (e.g. /data/data/my.app.lication/ )

I am currently using this method: myActivity.getFilesDir().getParent(); but it seems to me that this is a workaround when there is a simpler solution. In addition, a side effect is the creation of the files directory, which is not needed.

Clarification: firstly - Thanks for the defendants. I am trying to understand if there is already a method that does this, and not for another workflow.

+70
android directory
Apr 3 2018-11-11T00:
source share
7 answers
 PackageManager m = getPackageManager(); String s = getPackageName(); PackageInfo p = m.getPackageInfo(s, 0); s = p.applicationInfo.dataDir; 

If eclipse is worried about an 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); } 
+83
Apr 03 '11 at 6:01
source share

There is an easier 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 ()

+88
Oct 28 '13 at 8:50
source share

I got it

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

from here :

+20
Dec 09 '11 at 20:05
source share

Just use this in your code

  context.getApplicationInfo().dataDir 
+15
May 16 '16 at 15:55
source share

For the current Android application package:

 public String getDataDir(final Context context) throws Exception { return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.dataDir; } 

For any package:

 public String getAnyDataDir(final Context context, final String packageName) throws Exception { return context.getPackageManager().getPackageInfo(packageName, 0).applicationInfo.dataDir; } 
+9
Aug 29 '12 at 18:12
source share

If you are trying to access a file, try the openFileOutput() and openFileInput() methods, as described here . They automatically open I / O streams to the specified file in internal memory. This allows you to bypass the directory and File objects as a whole, which is a fairly clean solution.

+3
Apr 03 2018-11-11T00:
source share

Based on @ jared-burrows solution. For any package, but passing the context as a parameter ...

 public static String getDataDir(Context context) throws Exception { return context.getPackageManager() .getPackageInfo(context.getPackageName(), 0) .applicationInfo.dataDir; } 
+3
Dec 19 '12 at 12:35
source share



All Articles