无法在主外部存储中写入文件

时间:2016-08-31 00:56:43

标签: android

所以我没有外部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 个答案:

答案 0 :(得分:-1)

您不能这样做,您需要超级用户权限或root用户 检查这篇帖子已经回答了。

Trying to create a file in Android: open failed: EROFS (Read-only file system)