Android XML文件无法保存

时间:2012-07-11 02:44:07

标签: java android xml dom

我正在用LibGDX编写一个游戏,我正在尝试保存一个XML文件,但总有一个例外(java.io.FileNotFoundException:/ data /Slugfest / teams / Team1.xml:open failed:ENOENT (没有这样的文件或目录))保存文件时。此代码保存文件。

public void save() {
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result;

        if (Gdx.app.getType() == ApplicationType.Android) {
            result = new StreamResult(new File("/data/Slugfest/teams/" + name + ".xml"));
        } else {
            result = new StreamResult(new File(name + ".xml"));
        }
        transformer.transform(source, result);
        Gdx.app.log("Slugfest", "File saved.");
    } catch (TransformerException tfe) {
        Gdx.app.log("Slugfest", tfe.getLocalizedMessage());
    }
}

顺便说一句,我的清单文件包含WRITE / READ_EXTERNAL_STORAGE权限。

1 个答案:

答案 0 :(得分:1)

您需要创建要保存的目录。您应该检查它是否存在,如果没有,则创建它。像这样:

if (Environment.getExternalStorageState() == null) {
                directory = new File(Environment.getDataDirectory()
                        + "/RobotiumTestLog/");
                photoDirectory = new File(Environment.getDataDirectory()
                        + "/Robotium-Screenshots/");
                /*
                 * this checks to see if there are any previous test photo files
                 * if there are any photos, they are deleted for the sake of
                 * memory
                 */
                if (photoDirectory.exists()) {
                    File[] dirFiles = photoDirectory.listFiles();
                    if (dirFiles.length != 0) {
                        for (int ii = 0; ii <= dirFiles.length; ii++) {
                            dirFiles[ii].delete();
                        }
                    }
                }
                // if no directory exists, create new directory
                if (!directory.exists()) {
                    directory.mkdir();
                }

                // if phone DOES have sd card
            } else if (Environment.getExternalStorageState() != null) {
                // search for directory on SD card
                directory = new File(Environment.getExternalStorageDirectory()
                        + "/RobotiumTestLog/");
                photoDirectory = new File(
                        Environment.getExternalStorageDirectory()
                                + "/Robotium-Screenshots/");
                if (photoDirectory.exists()) {
                    File[] dirFiles = photoDirectory.listFiles();
                    if (dirFiles.length > 0) {
                        for (int ii = 0; ii < dirFiles.length; ii++) {
                            dirFiles[ii].delete();
                        }
                        dirFiles = null;
                    }
                }
                // if no directory exists, create new directory to store test
                // results
                if (!directory.exists()) {
                    directory.mkdir();
                }
            }

在这里我检查是否有SD卡,如果没有,我在本地保存,否则,我保存到SD。我也检查文件,如果它们在那里则删除。您可能不需要它,但这是一个全面的算法,应该做你需要的。你要什么就拿。

希望它有所帮助。