2016-08-31 80 views
0

所以我沒有外部SD卡,我想在我的設備內部存儲器的External分區中寫入文件。無法在主外部存儲器內寫入文件

這裏是我的代碼:

private String filename = "SampleFile.txt"; 
private String filepath = "MyFileStorage"; 
File mySensorData = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) 
    , filepath); 
    String myData = ""; 

private void FileWrite(String sensorReading) throws IOException { 
     if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) { 
      try { 
       FileOutputStream fos = new FileOutputStream(mySensorData); 
       fos.write(sensorReading.getBytes()); 
       fos.close(); 

       Toast.makeText(this, "File written to the external storage.", 
         Toast.LENGTH_SHORT).show(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     else { 

      Toast.makeText(this, "Unable to read the external storage.", 
        Toast.LENGTH_SHORT).show(); 
     } 
    } 

    //external storage discrepancies handler 
    private static boolean isExternalStorageReadOnly() { 
     String extStorageState = Environment.getExternalStorageState(); 
     if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) { 
      return true; 
     } 
     return false; 
    } 

    private static boolean isExternalStorageAvailable() { 
     String extStorageState = Environment.getExternalStorageState(); 
     if (Environment.MEDIA_MOUNTED.equals(extStorageState)) { 
      return true; 
     } 
     return false; 
    } 

我經常得到Unable to read the external storage.烤麪包和這個錯誤在顯示器上:

W/System.err: Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system) 

問題出在哪裏? 謝謝。

+1

嘗試getExternalStorageDirectory()而不是getExternalStoragePublicDirectory。或環境.DIRECTORY_MOVIES –

+0

@RandykaYudhistira不起作用! –

回答

相關問題