外部の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)
どこに問題がありますか?おかげさまで
代わりgetExternalStoragePublicDirectoryの)(getExternalStorageDirectoryを試してみてください。または環境.DIRECTORY_MOVIES –
@RandykaYudhistira動作しません! –