2010-12-01 61 views
1

我正嘗試在我的HTC Hero手機中將文件寫入我的SD卡。我使用以下命令在SDCard中創建文件:在SDCard上寫入問題 - Android

File = new File(path.getAbsolutePath(),「Filename.txt」);

其中path是我externalStorageDirectory(即\ SD卡)

當我登錄這個文件的路徑,但它說\ SD卡\ FILENAME.TXT

但是,當我創建一個FileOutputStream要寫入文件,突然將文件路徑更改爲\ data \ data,我無法訪問它。

有人可以請幫助澄清如何我可以在SDCard中創建一個文件,然後寫入它?

謝謝!

編輯:

path = Environment.getExternalStorageDirectory(); 
Log.d("SDCARDPLSWORK", path.toString()); 
    try 
    { 
     myFile = new File(path.getAbsolutePath(), "SensorValues.txt"); 
     boolean i = myFile.createNewFile(); 
     Log.d("SDCARDPLSWORK", myFile.toString() + " " + i); 
     fos = new FileOutputStream(myFile); 
     Log.d("FILEANDROID", getFileStreamPath("SensorValues.txt").toString()); 
    } 

    catch(FileNotFoundException e) 
    { 
     e.printStackTrace(); 
    } 

這是我在做什麼。直到SDCARDPLSWORK是正確的部分,但是當涉及到FILEANDROID日誌時,它會轉到專用數據存儲。

回答

0

使用SD卡路徑的Environment.getExternalStorageDirectory()

Environment.getExternalStorageDirectory()+"/yourFilename.png" 
+0

路徑= Environment.getExternalStorageDirectory(); Log.d(「SDCARDPLSWORK」,path.toString()); 嘗試 { \t myFile = new File(path.getAbsolutePath(),「SensorValues.txt」); \t布爾型i = myFile.createNewFile(); \t Log.d(「SDCARDPLSWORK」,myFile.toString()+「」+ i); \t fos = new FileOutputStream(myFile); \t Log.d(「FILEANDROID」,myFile。getAbsolutePath()); (FileNotFoundException e) } catch(FileNotFoundException e) { \t e.printStackTrace(); – Imelza 2010-12-01 08:18:48

+0

對不起,我在做什麼,我認爲,但它不工作。 – Imelza 2010-12-01 08:22:34

0

Environment.getExternalStorageDirectory()將獲取sdcard的根文件夾。

@JAVADOC

應用程序不應該直接使用 這個頂級目錄,以 避免污染用戶的根 命名空間。任何私密文件 應該放置在 返回的目錄Context.getExternalFilesDir,如果 卸載應用程序, 系統將負責刪除。其他 共享文件應放在 getExternalStoragePublicDirectory(String)返回的目錄中的一個 中。

/數據/數據/ ...這個位置是用來存儲文件爲你的包,所以嘗試下面的代碼:

File path = getApplicationContext().getFilesDir(); // get your private folder 
Log.d("SDCARDPLSWORK", path.toString()); 
File myFile; 
FileOutputStream fos; 
try { 
    myFile = new File(path.getAbsolutePath(), 
      "SensorValues.txt"); 
    boolean i = myFile.createNewFile(); 
    Log.d("SDCARDPLSWORK", myFile.toString() + " " + i); 
    fos = new FileOutputStream(myFile); 
    Log.d("FILEANDROID", getFileStreamPath("SensorValues.txt") 
      .toString()); 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
}