gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:53:12 27 4
gpt4 key购买 nike

在我的应用程序中,我打算将用户生成的文件存储在用户 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com