This code creates the file, but it's always empty.
I have seen some examples using a StringWriter, but my XML files can become so large, it doesn't seem practical saving it to a writer, converting it to a string, and then writing it to a file. It should be a stream.
Or perhaps I don't know what I'm talking about and have totally confused myself.
private void createXMLfile(File file, long folderRoot) throws IllegalArgumentException, IllegalStateException, IOException {
// https://developer.android.com/reference/org/xmlpull/v1/XmlSerializer
// https://stackoverflow.com/questions/5181294/how-to-create-xml-file-in-android
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(fileOutputStream, G.KML.XML_ENCODING); // XML_ENCODING = "UTF-8"
serializer.startDocument(G.KML.XML_ENCODING, Boolean.valueOf(true));
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startTag(null, G.KML.KML); // KML = "kml"
serializer.endTag(null, G.KML.KML);
serializer.endDocument();
serializer.flush();
fileOutputStream.flush();
fileOutputStream.close();
}
My Manifest permissions:
<!-- Following two permissions are for import/export functionality -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
This file is being written to:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
...but the user can change where the file gets saved.