java.io.FileNotFoundException:open failed:EROFS(只读文件系统)

时间:2014-11-24 11:35:07

标签: java android xml android-studio

我有这个代码生成一个XML文件并保存到外部目录。

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

创建XML文件后,引发了此异常

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

有什么方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

  

...保存到外部目录

这不太对。您正尝试将其保存在始终为只读的root系统分区中。

来自Environment.getRootDirectory()的javadoc:

  

返回"系统的根目录"分区持有核心Android操作系统。始终以只读方式存在和安装。

解决方案:只需将文件保存在其他位置:

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

请注意:

  • 您必须要求许可才能写入外部存储空间:

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

  • 外部存储并非始终可用。当您尝试在其上书写时,可能无法安装分区。 (小心)