简体   繁体   English

我应该在哪里选择将文本文件保存在android中?

[英]Where should I choose to save text file in android?

I hope to export my data as a text file and save it to disk in Android, so I need to choose which folder I will save the file to. 我希望将数据导出为文本文件并将其保存到Android中的磁盘,因此我需要选择将文件保存到的文件夹。

I hope that a normal user can find the folder easily and the app does not need special permission to create the folder. 我希望普通用户可以轻松找到该文件夹​​,并且该应用无需特殊权限即可创建该文件夹。

I have read some document, it seems that there are 3 ways: Context.getFilesDir().getAbsolutePath() , Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null) . 我已经阅读了一些文档,似乎有3种方法: Context.getFilesDir().getAbsolutePath()Environment.getExternalStorageDirectory().getAbsolutePath()Context.getExternalFilesDir(null)

You know some android users don't install SD card, so it seems that Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null) are be excluded. 您知道某些Android用户未安装SD卡,因此似乎排除了Environment.getExternalStorageDirectory().getAbsolutePath()Context.getExternalFilesDir(null)

Am I only to choose Context.getFilesDir().getAbsolutePath() ? 我只选择Context.getFilesDir().getAbsolutePath()吗? or is there a better way? 或者,还有更好的方法? Thanks! 谢谢!

BTW, From the document Android - Where to save text files to? 顺便说一句,从文档Android-要将文本文件保存到哪里?

Save it in internal phone storage, here no users and applications can access these files(unless if phone is rooted). 将其保存在内部电话存储中,此处没有用户和应用程序可以访问这些文件(除非电话已植根)。 But these files will be deleted one's the user selectes clear data from Settings -> Apps -> . 但是这些文件将被删除,用户可以从设置->应用程序->选择清除数据。

It seems that normal users can't access the saved text files if I use Context.getFilesDir().getAbsolutePath(), is it right? 如果我使用Context.getFilesDir()。getAbsolutePath(),似乎普通用户无法访问保存的文本文件,对吗?

Use this if you want a path that the user can modify and can have access 如果您想要用户可以修改并可以访问的路径,请使用此选项

getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();

More documentation here . 更多文档在这里

EDIT: 编辑:

This is how use in case error in some devices: 这是在某些设备中使用万一出现错误的方法:

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
String fname = "TEXT.txt";
File file = new File(path, fname);

if (!path.exists()) {
//noinspection ResultOfMethodCallIgnored
path.mkdir();
}

// YOUR CODE FOR COPY OR CREATE THE FILE TXT in PATH WITH THE VARIABLE file ABOVE

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

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