Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Application Directory

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.

like image 213
MByD Avatar asked Apr 03 '11 05:04

MByD


People also ask

How to get the folder of application c#?

Directory of windows forms application (.exe) It contains path of the .exe file (that started the application) including the executable file name. To get only the folder part of the path, use static method GetDirectoryName of Path class.

How to get file path in c# windows application?

To do this you can use Directory class: Directory. GetCurrentDirectory();

What is application StartupPath?

StartupPath property returns the path for the executable file that started the application, not the path where the application executable is stored. ExecutablePath property returns the path for the executable file that started the application, including the executable name.


2 Answers

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

like image 130
James Wald Avatar answered Sep 17 '22 23:09

James Wald


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); } 
like image 38
Philip Sheard Avatar answered Sep 16 '22 23:09

Philip Sheard