Android Studio:如何保存到文件

Android Studio: How to save to file

我正在执行此代码以将一些数据保存到我的 android phone 中的文件中,但是在保存它时 FileNotFoundException 触发器,我不知道为什么我从许多不同的地方复制但对我不起作用。

提示?

    public void safeToExcelFile(View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1000);
    }
    if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
        String fileName = "hola" + ".txt";
        String contenido = "pues algo a ver si va";

        File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS);
        File dir = new File(root.getAbsolutePath()+"/MyAppFile");
        if(!dir.exists()) {
            dir.mkdir();
        }
        File file = new File(dir, fileName);

        Toast.makeText(this, dir.getPath(), Toast.LENGTH_LONG).show();

        try{
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(contenido.getBytes());
            fos.close();
            Toast.makeText(this, "Se guardo", Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            Toast.makeText(this, "Archivo no encontrado", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        } catch (IOException e) {
            Toast.makeText(this, "Error al guardar", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }else{
        Toast.makeText(this, "No puedes guardar", Toast.LENGTH_LONG).show();
    }

}

带弟弟

requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1000);
    if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
        String fileName = "hola" + ".txt";
        String contenido = "pues algo a ver si va";

        String path = getExternalFilesDir(null) + "/" + fileName;
        try{
            FileOutputStream fos = new FileOutputStream(path);
            fos.write(contenido.getBytes());
            fos.close();
            Toast.makeText(this, "Se guardo", Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            Toast.makeText(this, "Archivo no encontrado", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        } catch (IOException e) {
            Toast.makeText(this, "Error al guardar", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }else{
        Log.d("Error", "No hay permiso");
    }

记录路径,您将看到文件的保存位置。这是存放本地文件的正确位置。

我终于找到了答案,如果你有兴趣,我从https://blog.cindypotvin.com/saving-data-to-a-file-in-your-android-application/那里得到了解决方案

   try {
        File testFile = new File(this.getExternalFilesDir(null), "Cuentas.csv");
        if (!testFile.exists())
            testFile.createNewFile();
        Toast.makeText(this, testFile.getPath(), Toast.LENGTH_LONG).show();

        // Adds a line to the file
        BufferedWriter writer = new BufferedWriter(new FileWriter(testFile, true));
        writer.write("This is a test file.");
        writer.close();
        // Refresh the data so it can seen when the device is plugged in a
        // computer. You may have to unplug and replug the device to see the
        // latest changes. This is not necessary if the user should not modify
        // the files.
        MediaScannerConnection.scanFile(this,
                new String[]{testFile.toString()},
                null,
                null);
    } catch (IOException e) {
        //Log.e("ReadWriteFile", "Unable to write to the TestFile.txt file.");
        Toast.makeText(this, "Error al guardar", Toast.LENGTH_LONG).show();
    }