How do I save files to external storage on Android (like san disk card)?

1

So the problem I'm facing is that I'm trying to save a .txt file to the Android memory stick, so when I try to run FileOutputStream.Write () it gives the following message:

  

/ mnt / media_rw / C4B3_13EE / Personal Folder / Documents / Textout.txt: open   failed: EACCES (Permission denied)

My Code :

FileOutputStream outputStream;
File file = new File("/mnt/media_rw/C4B3_13EE/Pasta Pessoal/Documentos/TextoSaída.txt");
try {
    outputStream = new FileOutputStream(file);
    outputStream.write(textView.getText().toString().getBytes());
    outputStream.close();
    Log.e("TAG", file.getAbsolutePath() + " Arquivo Criado");
} catch (IOException e) {
    Log.e("TAG", e.getMessage());
    e.printStackTrace();
}
MediaScannerConnection.scanFile(this,new String[] {file.getAbsolutePath()}, null, null);

I have already checked where there could be possible errors like:

  • Lack of permission in manifest:

    ...
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.febatis.texto">
    
            <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
            <application
                    ...
            </application>
    </manifest>
    
  • Do not request permission at runtime for read and write (Being called in the OnCreate () of my MainActivity:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.System.canWrite(this)) {
                    requestPermissions(new String[]{
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE
                    }, 0);
            }
    }
    
  • Do not test on directory without file and with file with the same existing name.

    File file = new File("/mnt/media_rw/C4B3_13EE/Pasta Pessoal/Documentos/TextoSaída.txt");
    File file = new File("/mnt/media_rw/C4B3_13EE/Pasta Pessoal/Documentos")
    

(Works for reading)

File file = new File("storage/C4B3_13EE/Pasta Pessoal/Documentos/TextoSaída.txt");

Well, I do not know what I can do to get access to the Android external memory card.

    
asked by anonymous 12.06.2017 / 07:22

0 answers