Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android app - how to write to Android device's Documents folder?

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.

I tried already:

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

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

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

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

Any help is appreciated, thanks!

like image 211
mr.burns Avatar asked Nov 21 '14 17:11

mr.burns


1 Answers

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
}
like image 73
TheoKanning Avatar answered Sep 25 '22 13:09

TheoKanning