Android - 创建自定义目录来存储文件

标签 android file storage

在我的应用程序中,我打算将用户生成的文件存储在用户 SD 卡存储中的自定义目录(我打算创建)中。

我正在使用:

String pathToExternalStorage = Environment.getExternalStorageDirectory().toString();
File appDirectory = new File(pathToExternalStorage + "/" + "AppName");
// have the object build the directory structure, if needed.
appDirectory.mkdirs();

您会推荐采用这种方法吗?如果用户删除我的应用程序,此文件夹和文件是否会保留?

因为,Android 文档说:

If you want to save files that are not specific to your application and that should not be deleted when your application is uninstalled, save them to one of the public directories on the external storage. These directories lay at the root of the external storage, such as Music/, Pictures/, Ringtones/, and others.

即使用户卸载应用程序,我也需要这些文件保留下来,但我不想在现有文件夹中添加我的文件时弄乱 - 我认为创建一个单独的文件夹会更好。我的目标是 API v9 及更高版本。

最佳答案

看起来是个不错的方法。我以类似的方式进行操作,除了我将所有内容都保存在数据文件夹中,即 sdcard/data/{packagename}

更新:

你不应该那样做。请改用 Environment.getDataDirectory()

关于Android - 创建自定义目录来存储文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7698277/

相关文章:

uwp - 调试时访问模拟器/设备的存储

java - 在java中,在磁盘上存储对象的最佳方式是什么?

android - ViewPager - setCurrentItem(int) 不滑动

java - 为什么会出现这些错误?

java - fragment 对话框动画正确但布局完全黑色

java - 使用流式传输时如何关闭Mule中FTP入站端点的输入流

php - 将数据从 MySql Server 传输到 Android App

c - 将文本文件读入 2 个单独的字符数组(在 C 中)

c++ - 用整数读取一行 C++

android - 在不下载完整操作系统的情况下在eclipse中安装android sdk