java openFileOutput:如何在 /data/data.... 路径之外创建文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17022221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:44:40  来源:igfitidea点击:

openFileOutput: How to create files outside the /data/data.... path

javaandroid

提问by afkiwers

I wonder whether you can help me out with this question. I don't understand how I can access e.g. the "Download" folder or some of my own folder.

我想知道你是否能帮我解决这个问题。我不明白如何访问例如“下载”文件夹或我自己的一些文件夹。

I want to create some txt-files and have access to it over USB. I didn't find a topic related to my question cause I don't know where exactly I am searching for.

我想创建一些 txt 文件并可以通过 USB 访问它。我没有找到与我的问题相关的主题,因为我不知道我到底在搜索哪里。

        String string = "hello world!";

        FileOutputStream fos = openFileOutput("test.txt", Context.MODE_PRIVATE);
        fos.write(string.getBytes());
        fos.close();

Thanks for hints :)

感谢您的提示:)

回答by britzl

Start by reading the official documentation on file storage options. Remember that external storage doesn't equate to "removable SD-card". It can just as easily be the 32Gb or whatever memory you have on your Nexus device.

首先阅读有关文件存储选项的官方文档。请记住,外部存储并不等同于“可移动 SD 卡”。它可以轻松成为 32Gb 或您 Nexus 设备上的任何内存。

Here's an example of how to get the base folder of the files dir (ie the folder that gets deleted when you uninstall the app, as opposed to the cache dir that still exist even after uninstall):

这是如何获取文件目录的基本文件夹的示例(即卸载应用程序时被删除的文件夹,而不是即使卸载后仍然存在的缓存目录):

String baseFolder;
// check if external storage is available
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
    baseFolder = context.getExternalFilesDir(null).getAbsolutePath()
}
// revert to using internal storage
else {
    baseFolder = context.getFilesDir().getAbsolutePath();
}

String string = "hello world!";
File file = new File(basefolder + "test.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(string.getBytes());
fos.close();

UPDATE: Since you require the file to be accessible via USB and your PCs file manager instead of DDMS or similar you could use the Environment.getExternalStoragePublicDirectory()and pass Environment.DIRECTORY_DOWNLOADSas argument (note that I'm not sure if there's an equivalent for the internal storage):

更新:由于您需要通过 USB 和您的 PC 文件管理器而不是 DDMS 或类似方式访问该文件,您可以使用Environment.getExternalStoragePublicDirectory()并将Environment.DIRECTORY_DOWNLOADS作为参数传递(请注意,我不确定是否有等效的用于内部存储):

String baseFolder;
// check if external storage is available
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
    baseFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
}
// revert to using internal storage (not sure if there's an equivalent to the above)
else {
    baseFolder = context.getFilesDir().getAbsolutePath();
}

String string = "hello world!";
File file = new File(basefolder + File.separator + "test.txt");
file.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(file);
fos.write(string.getBytes());
fos.flush();
fos.close();