How to save files in Android external storage (like san disk card)?

Asked

Viewed 402 times

1

So the problem I’m facing is that I’m wanting to save a file. txt on the Android memory card, it turns out that when I try to run Fileoutputstream.Write() it gives the following message:

/mnt/media_rw/C4B3_13EE/Personal Folder/Documents/Textosaída.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’ve checked where there could be possible mistakes like:

  • The lack of permission in the 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>
    
  • Not having asked permission at runtime to read and write (Being called in Oncreate() from 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);
            }
    }
    
  • Have not tested in 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 don’t know what I can do to gain access to Android external memory card.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.