安卓获取应用程序的“家”数据目录应用程序、目录、数据

2023-09-12 08:41:39 作者:路人甲

有一个简单的问题,涉及到默认的'家'目录时,一个应用程序写入到内存中。默认情况下,创建的任何文件都放置在OS(2.2):

A simple question, relating to the default 'home' directory when an app writes to the internal memory. By default, any files created are placed by the OS (2.2) in:

/data/data/your.package/files

当读取文件时,同样使用默认值,通过 openFileInput在适当的范围内保持()的时候 openFileOutput()。但是,如果我需要检查文件是否存在,例如,使用文件级的,我需要指定在构造的完整路径。

When reading in files, the same default is used, when keeping in proper context via openFileInput(), openFileOutput(). But if I need to check file existence, for instance, using the File class, I need to specify the whole path in the constructor.

我看到有 Environment.getDataDirectory()(返回 /数据), Environment.getRootDirectory()(返回 /系统)等,但没有涉及到获取应用程序的家目录。

I see there are Environment.getDataDirectory() (returns /data), Environment.getRootDirectory() (returns /system), etc, but nothing related to getting the app's 'home' directory.

这不是一个巨大的交易,但我c中的完整路径为我的应用程序的文件使用,而不是硬$ C $(比如包名称的变化,说在未来的操作系统版本的路径变化),如果有一些方法以编程方式引用了应用程序的家目录。

It's not a huge deal, but I'd rather not hard-code the full path into my App for File to use (say the package name changes, say the path changes in a future OS release) if there is some way to reference the app's 'home' directory programmatically.

推荐答案

当然,永远不会失败。发现了大约一分钟的解决方案张贴上述问题后......对于那些解决方案可能有同样的问题:

Of course, never fails. Found the solution about a minute after posting the above question... solution for those that may have had the same issue:

ContextWrapper.getFilesDir()

找到的here.