簡體   English   中英

Android XML文件無法保存

[英]Android XML file doesn't save

我正在用LibGDX編寫游戲,並且試圖保存XML文件,但是總是有一個異常(java.io.FileNotFoundException:/data/Slugfest/teams/Team1.xml:打開失敗: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權限。

您需要創建要保存到的目錄。 您應該檢查它是否存在,如果沒有,請創建它。 像這樣:

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。 我還會檢查文件,然后刪除是否存在。 您可能不需要它,但這是一個綜合算法,可以完成您需要的操作。 你要什么就拿。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM