Get permission to write to SD Card on Android 6+

0

I'm creating an app that reads and writes files to Android, so I can not get permission to write to the device's external storage. After searching I saw that I need to call this screen to get permission, but I do not know what it's called there.

TheseimagesIfoundontheWebandhavealsoseenappslikeWordoresFileExplorerasking.

SoI'mrequestingtheexternalreadandwritepermissionsontheOnCreateofmyMainActivitythisway:

if(ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED||ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED){if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){ActivityCompat.requestPermissions(this,newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},0);}}

Toreadfilesfrombothemulated/0/sdcard/(internalstorage)and/mnt/media_rw/C4B3_13EE/(externalstorage(SDcardinstalledonthedevice))Icanuploadmyfiletomyapplication

Nowtowriteisabitmorecomplicated,emulated/0/sdcard/evenwithyou,butin/mnt/media_rw/C4B3_13EE/givesanexception,see:

  

E/TAG:/mnt/media_rw/C4B3_13EE/Folder  Personal/Documents/TextOutput.txt:openfailed:EACCES(Permission  denied)

Mywritingcodeisjustbelow

FileOutputStreamoutputStream;Filefile=newFile("/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", file.getAbsolutePath() + " Arquivo NÃO Criado");
        Log.e("TAG", e.getMessage());
        e.printStackTrace();
}
MediaScannerConnection.scanFile(this,new String[] {file.getAbsolutePath()}, null, null);
    
asked by anonymous 17.06.2017 / 09:09

1 answer

1

Well, it was really hard but I did it. I discovered that in android version 4.4 (Kitkat) a SAF (Storage Access Framework) was introduced, the android developer website explains better and has the snippets I used to solve part of my problem, see:

  • I was using the ACTION_GET_CONTENT intent (which gets a copy of the desired file), to get the file I wanted to edit, however google advises to use ACTION_OPEN_DOCUMENT if I want to have a persistent access (Create, edit, delete) even on SD Card.
  • With that done, I came up with another challenge, when I saved the edited file, the windows notepad does not "see" the line breaks we put in the android. To solve this question I made this little code:

    String texto = textView.getText().toString();
    String separador = "\n";
    String[] linhas = texto.split(separador);
    

    With each line obtained at the time of writing, I just modified the snippet in the Editing section of a document by placing a foreach:

    for (String linha : linhas)
        fileOutputStream.write((linha + "\r\n").getBytes());
    

    So Windows's native notepad can read each line individually.

    Note: If you make any changes to any file while the android device is connected in MTP mode, you must remove it (or switch to just by charging) and reconnect it to your computer. The same procedure applies to Linux (at least in Ubuntu 16 I had to do it).

    Sources:
    link   link

        
    18.06.2017 / 21:03