Android应用程序-如何写入Android设备的“文档”文件夹? [英] Android app - how to write to Android device's Documents folder?

查看:86
本文介绍了Android应用程序-如何写入Android设备的“文档”文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Android应用程序中创建一个XML文件。
这个文件我想写到我的Android设备的documents文件夹中。
之后,我想使用USB将Android设备连接到PC,然后从documents文件夹中读取该XML文件。
我的设备是Android Galaxy Tab Pro 10.1,Android 4.4.2。

I want to create a XML file inside my Android app. This file I want to write into the documents folder of my Android device. Later I want to connect my Android device to my PC using USB and read that XML file out of the documents folder. My Device is an Android Galaxy Tab Pro 10.1, Android 4.4.2.

我已经尝试过:

String fileName = "example.xml";
String myDirectory = "myDirectory";

String externalStorage = Environment.getExternalStorageDirectory().getAbsolutePath();

File outputFile = new File(externalStorage + File.separator + myDirectory + File.separator + fileName);

但未创建文件。我还想稍后再将该文件从Documents文件夹中再次读入may应用程序。

But no file is created. I also want later to read that file out of the documents folder into may app again.

感谢您的帮助!

推荐答案

我知道已经很晚了,但是您可以像这样获得documents目录:

I know this is late, but you can get the documents directory like this:

File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
File file = new File(dir, "example.txt");

//Write to file
try (FileWriter fileWriter = new FileWriter(file)) {
fileWriter.append("Writing to file!");
} catch (IOException e) {
    //Handle exception
}

这篇关于Android应用程序-如何写入Android设备的“文档”文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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