简体   繁体   English

Android - 如何在内部存储中创建文件夹/文件

[英]Android - How to create folders/files in internal storage

I want to be able to create a folder for my app within "Internal Storage/" (on startup, if required) and later, within this folder, create any files (.txt) the user creates in the app.我希望能够在“内部存储/”(启动时,如果需要)中为我的应用程序创建一个文件夹,然后在此文件夹中创建用户在应用程序中创建的任何文件 (.txt)。

These files must remain if the app is uninstalled, so use of getExternalFilesDir() - while it works - is not possible.如果应用程序被卸载,这些文件必须保留,因此无法使用 getExternalFilesDir() - 虽然它可以工作 - 是不可能的。 I should note I don't want the user to have to use the file picker to create the folder or any files - ideally the app will take care of saving it to the app's public folder for them.我应该注意我不希望用户必须使用文件选择器来创建文件夹或任何文件 - 理想情况下,应用程序会负责将其保存到应用程序的公共文件夹中。 So far I've tried using MediaStore and Storage Access Framework but have had little success.到目前为止,我已经尝试使用 MediaStore 和 Storage Access Framework,但收效甚微。 Just need a push in the right direction.只需要朝着正确的方向推动。

Note: I'm aware of getExternalStoragePublicDirectory().注意:我知道 getExternalStoragePublicDirectory()。 It worked for what I need quite well, but I'd prefer not to use deprecated functions if possible.它非常适合我需要的东西,但如果可能的话,我宁愿不使用不推荐使用的函数。 I also have the required permission in my manifest file, just having a hard time creating these files outside of my app's own folder within /data/我的清单文件中也有所需的权限,只是很难在 /data/ 中我的应用程序自己的文件夹之外创建这些文件

Thanks in advance anyone who can help :)预先感谢任何可以提供帮助的人:)

Try this,尝试这个,

private fun getInternalLogFile(filename: String): File? =
            File(
                    context.getDir(INTERNAL_DIRECTORY_NAME, Context.MODE_PRIVATE),
                    filename
            )

INTERNAL_DIRECTORY_NAME is your folder name INTERNAL_DIRECTORY_NAME 是您的文件夹名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM