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

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

问题描述

有人知道如何获取我的应用程序目录的路径吗?(例如 /data/data/my.app.lication/)

Does someone know how do I get the path of my application directory? (e.g. /data/data/my.app.lication/)

目前我正在使用此方法:myActivity.getFilesDir().getParent(); 但在我看来,当有更简单的解决方案时,它是一种解决方法.此外,副作用是创建了不需要的 files 目录.

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;

如果eclipse担心未捕获的NameNotFoundException,您可以使用:

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天全站免登陆