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);