如何在android上的外部存储上写文件到文件? [英] How to write text to a file on external storage on android?

查看:155
本文介绍了如何在android上的外部存储上写文件到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将一些文本写入Android设备上的文件似乎是一项重大努力。从这里给出的答案开始,我有在我的单个'Hello-World'活动中实现了以下代码:

Writing some text to a file on an android device seems to be a major endeavor. Starting from an answer given here I have implemented the following code into my single '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上看到使用文件管理器创建的文件?代码片段似乎写入android文件系统上与应用程序本身相关的秘密位置(使用 this.openFileOutput 进行管理)。

which does not throw an exception, but seems to work. But is there a way to 'see' the file created with the File Manager on android? The code snippet seems to write to a 'secret' location on the android file system related to the application itself (governed by using this.openFileOutput).

咨询不同的谷歌链接(这个 和这个 这个)我想出来了以下代码:

Consulting different google-links (this and this and this) I come up with the following code:

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)

那么如何做到这一点呢?

So how to do this right?

作为旁注:这仅用于调试/教育目的,并非旨在成为最终应用程序的一部分!

As a side note: This is for debugging/educational purposes only and not intended to be part of a final app!

要明确:我想在我用FileManager看到的 temp 目录中创建一个文件( temp 目录与我的文档
Music DCIM 下载等......)

To be clear: I want to create a file inside the temp directory I can see with a FileManager (the temp directory is on the same level as My Documents , Music, DCIM, Download and the like...)

推荐答案


但有没有办法'看'用android上的文件管理器创建的文件?

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

我不确定你指的是什么文件管理器,因为Android没有真正的文件管理器。但是,您正在写一个应用程序的内部存储空间,这是应用程序的私有。除了在root设备上和通过一些繁琐的Android SDK工具之外,其他应用程序或普通用户无法查看它。

I am not certain what "the File Manager" is that you are referring to, as Android does not really have one. However, you are writing to an app's internal storage, and that is private to the app. It cannot be viewed by other apps or ordinary users, except on rooted devices and via some cumbersome Android SDK tools.


那么如何做到这一点对?

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();

此外:


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

  • Please do this work on a background thread

在Android 4.3(API级别18)和旧设备上,您需要按住 WRITE_EXTERNAL_STORAGE 使用外部存储空间

On Android 4.3 (API Level 18) and older devices, you need to hold the WRITE_EXTERNAL_STORAGE permission to work with external storage

如果您还希望快速显示此文件-device或on-development-machine文件管理器,在关闭文件后使用它:

If you also want to have this file show up quickly in on-device or on-development-machine file managers, use this after closing the file:

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

(其中是一些上下文,例如活动服务

(where this is some Context, such as an Activity or Service)

这篇关于如何在android上的外部存储上写文件到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆