2013-02-26 99 views
0

我正在研究將XML文件寫入設備的方法。我已經在清單文件中允許外部存儲,但是我無法在它應該在的位置找到該文件。Android:找不到生成的XML文件

這裏是我的代碼:

public static void write(){ 
Serializer serial = new Persister(); 
File sdcardFile = new File("/Prueba/file.xml"); 
    Item respuestas = new Item(); 

    try { 
serial.write(respuestas, sdcardFile); 
    } catch (Exception e) { 
// There is the possibility of error for a number of reasons. Handle this  appropriately in your code 
e.printStackTrace(); 
     } 
     Log.i(TAG, "XML Written to File: " + sdcardFile.getAbsolutePath()); 
    } 

    } 

回答

1

SD卡的文件路徑問題。這是一個在file.xml文件中寫入字符串的例子。

File myFile = new File("/sdcard/file.xml"); 



try { 
      File myFile = new File("/sdcard/file.xml"); 
      myFile.createNewFile(); 
      FileOutputStream fOut = new FileOutputStream(myFile); 
      OutputStreamWriter myOutWriter = 
            new OutputStreamWriter(fOut); 
      myOutWriter.append("encodedString"); 
      myOutWriter.close(); 
      fOut.close(); 

     } catch (Exception e) { 

     } 

您可以通過這種方式來獲得外部存儲姓名,

String root = Environment.getExternalStorageDirectory().toString(); 
+0

謝謝你現在的工作 – Katherine99 2013-02-26 09:50:39