获取应用程序目录 [英] Get Application Directory

查看:127
本文介绍了获取应用程序目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人知道如何让我的应用程序目录的路径? (例如: /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);
}

这篇关于获取应用程序目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆