Mark Cortejo
Mark Cortejo

Reputation: 109

java.io.FileNotFoundException: open failed: EROFS (Read-only file system)

I have this code that generates an XML file and saves to an external directory.

    try //database structure
    {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        //test report elements, does not reflect the real database in the future
        //mandatory/ username, location, caption, media, time(Actually, it's best if the server determines recieve time)
        //optional/
        //reportdata elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("reportdata");
        doc.appendChild(rootElement);

        //username elements
        Element username = doc.createElement("username");
        username.appendChild(doc.createTextNode("testusername"));
        rootElement.appendChild(username);

        //Location ELEMENTS
        //latitude elements
        Element lat = doc.createElement("latitude");
        lat.appendChild(doc.createTextNode(String.valueOf(latitude)));
        rootElement.appendChild(lat);
        //longitude
        Element longi = doc.createElement("longitude");
        longi.appendChild(doc.createTextNode(String.valueOf(longitude)));
        rootElement.appendChild(longi);

        //caption text elements

        Element capt = doc.createElement("caption");
        capt.appendChild(doc.createTextNode(captionText.getText().toString()));
        rootElement.appendChild(capt);

        //tag elements
        String[] tagArr = new String[selectItems.size()];
        tagArr = selectItems.toArray(tagArr);
        Element tags = doc.createElement("tags");
        rootElement.appendChild(tags);
        int o = selectItems.size();
        for(String s: tagArr) {
            Element tagname = doc.createElement("tagname");
            tagname.appendChild(doc.createTextNode(s));
            tags.appendChild(tagname);
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        //StreamResult result = new StreamResult(System.out);
        StreamResult result = new StreamResult(new File(android.os.Environment.getRootDirectory(), "upload_data.xml"));
        transformer.transform(source, result);
        Log.d("MESSAGE", result.toString());

After the creation of the XML file, this exception was raised

D/XML TransformerException﹕ java.io.FileNotFoundException: /system/upload_data.xml: open failed: EROFS (Read-only file system)

Any way to fix this?

Upvotes: 2

Views: 17627

Answers (1)

ben75
ben75

Reputation: 28706

... saves to an external directory

This is not quite right. You are trying to save it in the root system partition which is always read-only.

From javadoc of Environment.getRootDirectory():

Return root of the "system" partition holding the core Android OS. Always present and mounted read-only.

Solution : just save your file somewhere else :

StreamResult result = new StreamResult(new File(android.os.Environment.getExternalStorageDirectory(), "upload_data.xml"));

Note that :

  • you must ask the permission to write to the external storage :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  • External storage is not always available. The partition may not be mounted -yet- when you try to write on it. (just be careful)

Upvotes: 7

Related Questions