2012-07-11 163 views
1

我正在用LibGDX編寫遊戲,我試圖保存一個XML文件,但總是有一個異常(java.io.FileNotFoundException:/ data/Slugfest/teams/Team1 .xml:打開失敗:ENOENT(無此文件或目錄))保存文件時。該代碼保存文件。Android XML文件不保存

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

我不知道'LibGDX',但在簡單的android的情況下,你需要做一個dir,如果它不可用。像'File dir = new File(「path to file」);'然後 if(dir.exists()== false){dir.exists()== false){dir.mkdirs(); } 然後用於文件 File file = new File(dir,fileName); – 2012-07-11 02:50:52

回答

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。我也檢查文件,如果他們在那裏刪除。你可能不需要,但這是一個全面的算法,應該做你需要的。你要什麼就拿。

希望它有幫助。

+1

謝謝!這完美的作品! – Shane 2012-07-11 03:34:57

+0

不客氣!很高興我能幫上忙!隨意接受和upvote,如果你喜歡:) – BlackHatSamurai 2012-07-11 03:35:30