2017-02-20 138 views
1

我想寫一個.csv文件到我已連接到我的android digiland平板電腦的micro SD卡上。每當我嘗試向SD卡寫入文件時,將SD卡插入計算機時唯一顯示的文件是名爲lost.dir的空文件夾。我對此做了一些研究,聽起來像lost.dir是一些數據丟失時創建的文件夾。我不確定數據爲什麼會丟失。我一定要正確地安裝和卸載SD卡,並且我不知道如何解決此問題。這裏是我使用的代碼:在Android上寫文件到SD卡(lost.dir)

String string = 
      " \n" + teamNum + "," + matchNum + ","+ gearPoints + "," + climbScored 
        + "," + ballsHigh + "," + ballsLow + "," + gearsAuto + "," + 
      hiBallsAuto + "," + lowBallsAuto + "," + driveForward + ""; 

    if(isSdWriteable()) { 
     File sdCard = Environment.getExternalStorageDirectory(); 
     File file = new File(sdCard, "scout9000.csv"); 

     try { 
      FileOutputStream outputStream = new FileOutputStream(file); 
      outputStream.write(string.getBytes()); 
      outputStream.close(); 
     } catch (Exception e) { 
      System.out.println("Error writing file."); 
     } 
    } 

這裏是我的isSdWriteable功能:

public boolean isSdWriteable() { 
    String state = Environment.getExternalStorageState(); 
    if(Environment.MEDIA_MOUNTED.equals(state)) { 
     return true; 
    } 
    return false; 
} 

任何幫助表示讚賞。

+0

IIRC lost.dir創建每次使用Android插任何儲存時間 – Qwertie

回答

0

Environment.getExternalStorageDirectory()並不意味着你的外部SD卡。 See API ref here

注意:不要被「外部」一詞混淆。該目錄 可以更好地被認爲是媒體/共享存儲。它是一個文件系統,它可以容納相對較大數量的數據,並在所有應用程序間共享(不強制執行權限)。傳統上,這是SD卡的 ,但它也可以作爲 設備中的內置存儲設備實現,該設備與受保護的內部存儲設備不同,並且可以將 作爲文件系統安裝在計算機上。

讓您的外置SD卡更爲複雜 - 看到存在的問題在這裏: How can I get external SD card path for Android 4.0+?