获取应用程序目录应用程序、目录

2023-09-12 01:10:09 作者:放不下你是我活该

是否有人知道如何让我的应用程序目录的路径? (例如: /data/data/my.app.lication /

目前我使用这个方法: myActivity.getFilesDir()的getParent(); ,但在我看来,像一个解决办法时,有一个简单的解决方案。此外,副作用是创作的文件目录,这是联合国需要。

澄清:的第一 - 谢谢你的repliers。我试着去了解,如果有已经存在的方法做它,而不是另一种解决方法。

解决方案

  PackageManager M = getPackageManager();
字符串s = getPackageName();
PackageInfo p值= m.getPackageInfo(S,0);
S = p.applicationInfo.dataDir;
 
怎么查找应用程序出错的位置

如果有关日食的忧虑未捕获的的NameNotFoundException ,你可以使用:

  PackageManager M = getPackageManager();
字符串s = getPackageName();
尝试 {
    PackageInfo p值= m.getPackageInfo(S,0);
    S = p.applicationInfo.dataDir;
}赶上(PackageManager.NameNotFoundException E){
    Log.w(yourtag,错误包找不到名称,E);
}
 

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.

解决方案

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