Android 获取应用程序的“主页"数据目录 [英] Android Get Application's 'Home' Data Directory

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

问题描述

一个简单的问题,与应用程序写入内部存储器时的默认主"目录有关.默认情况下,操作系统 (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() 保持在适当的上下文中.但是如果我需要检查文件是否存在,例如使用 File 类,我需要在构造函数中指定整个路径.

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()(返回/data),Environment.getRootDirectory()(返回/system) 等,但与获取应用的主"目录无关.

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

这不是什么大问题,但如果有某种方法,我宁愿不将完整路径硬编码到我的应用程序中以供文件使用(比如包名更改,比如未来操作系统版本中的路径更改)以编程方式引用应用的主"目录.

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

此处找到.

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

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