Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法在主外部存储器中写入文件_Android - Fatal编程技术网

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

Android 无法在主外部存储器中写入文件,android,Android,因此,我没有外部SD卡,我想将文件写入设备内部存储器的外部分区中 这是我的密码: private String filename = "SampleFile.txt"; private String filepath = "MyFileStorage"; File mySensorData = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) , filepath);

因此,我没有外部SD卡,我想将文件写入设备内部存储器的外部分区中

这是我的密码:

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不行!