Android 无法在主外部存储器中写入文件
因此,我没有外部SD卡,我想将文件写入设备内部存储器的外部分区中 这是我的密码:Android 无法在主外部存储器中写入文件,android,Android,因此,我没有外部SD卡,我想将文件写入设备内部存储器的外部分区中 这是我的密码: private String filename = "SampleFile.txt"; private String filepath = "MyFileStorage"; File mySensorData = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) , filepath);
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;
}
我经常遇到无法读取外部存储器的问题。toast和监视器上的此错误:
W/System.err: Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)
问题在哪里?
谢谢。您不能这样做,您需要超级用户权限或root用户权限 检查此帖子是否已回复
尝试getExternalStorageDirectory而不是getExternalStoragePublicDirectory。或Environment.DIRECTORY_MOVIES@RandykaYudhistira不行!