java - 如何将文本写入Android外部存储上的文件?

标签 java android

将一些文本写入 Android 设备上的文件似乎是一项重大努力。从 an answer given here 开始我已将以下代码实现到我的单个“Hello-World” Activity 中:

try {
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(this.openFileOutput("config.txt", Context.MODE_PRIVATE));
    outputStreamWriter.write("lalala");
    outputStreamWriter.close();
} catch (IOException e) {
    Log.e("Exception", "File write failed: " + e.toString());
}

它不会抛出异常,但似乎有效。但是有没有办法在 android 上“查看”使用 File Manager 创建的文件?代码 fragment 似乎写入了与应用程序本身相关的 android 文件系统上的“ secret ”位置(通过使用 this.openFileOutput 控制)。

咨询不同的谷歌链接( this and this and this )我想出了以下代码:

File file = new File(this.getExternalFilesDir("temp"), "testfile.txt");
FileOutputStream fileOutput = openFileOutput(file.getName(), Context.MODE_WORLD_WRITEABLE);
fileOutput.write("lalala");
fileOutput.close();

抛出错误

Error:(55, 19) error: no suitable method found for write(String) method FileOutputStream.write(int) is not applicable (actual argument String cannot be converted to int by method invocation conversion) method FileOutputStream.write(byte[],int,int) is not applicable (actual and formal argument lists differ in length) method OutputStream.write(int) is not applicable (actual argument String cannot be converted to int by method invocation conversion) method OutputStream.write(byte[],int,int) is not applicable (actual and formal argument lists differ in length) method OutputStream.write(byte[]) is not applicable (actual argument String cannot be converted to byte[] by method invocation conversion)

那么如何正确地做到这一点呢?

附带说明:这仅用于调试/教育目的,不打算成为最终应用程序的一部分!

明确一点:我想在 temp 目录中创建一个文件,我可以使用 FileManager 看到该目录(temp 目录与 处于同一级别)我的文档 , Music, DCIM, Download 等等...)

最佳答案

But is there a way to 'see' the file created with the File Manager on android?

我不确定您指的“文件管理器”是什么,因为 Android 并没有真正的文件管理器。但是,您正在写入应用程序的 internal storage ,这对应用程序是私有(private)的。它不能被其他应用程序或普通用户查看,除非在已获得 root 权限的设备上和通过一些繁琐的 Android SDK 工具。

So how to do this right?

File file = new File(this.getExternalFilesDir(null), "testfile.txt");
FileOutputStream fileOutput = new FileOutputStream(file);
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutput);
outputStreamWriter.write("lalala");
outputStreamWriter.flush();
fileOutput.getFD().sync();
outputStreamWriter.close();

还有:

  • 请在后台线程上完成这项工作

  • 在 Android 4.3(API 级别 18)和更早的设备上,您需要持有 WRITE_EXTERNAL_STORAGE 权限才能使用 external storage

如果您还想让这个文件快速显示在设备上或开发机器上的文件管理器中,请在关闭文件后使用:

MediaScannerConnection.scanFile(
  this,
  new String[]{file.getAbsolutePath()},
  null,
  null);

(其中 this 是一些 Context,例如 ActivityService)

关于java - 如何将文本写入Android外部存储上的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37797650/

相关文章:

android - 删除本地存储但仍然存在

安卓穿戴操作系统

java - 在复合键 hibernate xml 中映射复合键

java - 多边形面积

java - session 值未存储在使用 Chrome 的 wicket 应用程序中

android - Firebase Messaging - 在应用程序处于后台时创建抬头显示

java getBytes 错误

java - 自定义 Volley 请求上的 NullPointerException

java - 需要从delphi应用程序读取java运行时jar文件

java - 防止AMQP队列消费者中的无限循环