2011-08-23 97 views
1

在我的Android應用程序中,我嘗試在SD卡上創建一個xml文件。我嘗試這樣做:createNewFile()拋出錯誤

public class XmlFileCreator extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     System.out.println(Environment.getExternalStorageDirectory()); 
     // create a new file called "new.xml" in the SD card 
     File newxmlfile = new File(Environment.getExternalStorageDirectory(),"new.xml"); 
     try { 
      newxmlfile.createNewFile(); 
     } catch (IOException e) { 
      Log.e("IOException", "exception in createNewFile() method"); 
     } 

     FileOutputStream fileos = null; 
     try { 
      fileos = new FileOutputStream(newxmlfile); 
     } catch (FileNotFoundException e) { 
      Log.e("FileNotFoundException", "can't create FileOutputStream"); 
     } 
     // we create a XmlSerializer in order to write xml data 
     XmlSerializer serializer = Xml.newSerializer(); 

     try { 
      // we set the FileOutputStream as output for the serializer, using 
      // UTF-8 encoding 
      serializer.setOutput(fileos, "UTF-8"); 
      // Write <?xml declaration with encoding (if encoding not null) and 
      // standalone flag (if standalone not null) 
      serializer.startDocument(null, Boolean.valueOf(true)); 
      // set indentation option 
      serializer.setFeature(
        "http://xmlpull.org/v1/doc/features.html#indent-output", 
        true); 
      // start a tag called "root" 
      serializer.startTag(null, "root"); 
      // i indent code just to have a view similar to xml-tree 
      serializer.startTag(null, "child1"); 
      serializer.endTag(null, "child1"); 
      serializer.startTag(null, "child2"); 
      // set an attribute called "attribute" with a "value" for <child2> 
      serializer.attribute(null, "attribute", "value"); 
      serializer.endTag(null, "child2"); 

      serializer.startTag(null, "child3"); 
      // write some text inside <child3> 
      serializer.text("some text inside child3"); 
      serializer.endTag(null, "child3"); 

      serializer.endTag(null, "root"); 
      serializer.endDocument(); 
      // write xml data into the FileOutputStream 
      serializer.flush(); 
      // finally we close the file stream 
      fileos.close(); 

      System.out.println("file has been created on SD card");   
     } 
     catch(Exception e){ 
      Log.e("Exception","error occurred while creating xml file"); 
     } 
    } 
} 

但在logcat中我得到這個:

08-23 11:19:56.776: ERROR/IOException(6333): exception in createNewFile() method 
08-23 11:19:56.776: ERROR/FileNotFoundException(6333): can't create FileOutputStream 
08-23 11:19:56.776: ERROR/Exception(6333): error occurred while creating xml file 

問題出在哪裏?爲什麼createNewFile()拋出IOException?需要一些幫助,請...

+0

將是很好的日誌實際例外和他們的消息... – Thilo

回答

2

檢查一次你提到的權限在外部存儲寫manifast文件或不

+0

謝謝:D ...我忘了把權限。 – Gabrielle

5
File newxmlfile = new File(Environment.getExternalStorageDirectory()+"/new.xml"); 

清單許可

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

在大多數手機上提供WRITE_EXTERNAL_STORAGE權限已足夠。在運行Android 4.1.1的Sony XPeria E上如何運行我已經等待給出READ_EXTERNAL_STORAGE權限。 –