0
I’m creating an app that reads and writes files on Android, it turns out I can’t get permission to record on the device’s external storage. After researching I saw that I need to call this screen to get permission, but I do not know what it calls there.
These images I found on the web and I’ve also seen apps like Word or esFileExplorer asking.
So I’m asking for write and external reading permissions on my Mainactivity’s Oncreate this way:
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,
new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
}, 0);
}
}
To read files from both Emulated/0/sdcard/ (internal storage) and /mnt/media_rw/C4B3_13EE/ (external storage (SD card installed on the device)) I can upload the file to my application
Now to write is a little more complicated, in Emulated/0/sdcard/ up to you, but in /mnt/media_rw/C4B3_13EE/ gives an Exception, see:
E/TAG: /mnt/media_rw/C4B3_13EE/Pasta Personal/Documents/Textosaída.txt: open failed: EACCES (Permission denied)
My code for writing is just below
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", file.getAbsolutePath() + " Arquivo NÃO Criado");
Log.e("TAG", e.getMessage());
e.printStackTrace();
}
MediaScannerConnection.scanFile(this,new String[] {file.getAbsolutePath()}, null, null);
You only need to ask for permission at runtime, use the lib Reactivepermissions de Max Cruz: https://github.com/MaxCruz/reactive_permissions.
– Arthur Silva
This lib only asks for permissions. I’m asking for permissions. the problem is that it actually gives Permission denied
– Lucas Batista
To be sure that the problem is the "runtime permissions" on the definitions -> applications choose your application and manually assign.
– ramaral
Done. The Storage switch is enabled
– Lucas Batista
The error continues?
– ramaral
Yes. It was already enabled before. My application asks for runtime permissions correctly.
– Lucas Batista
I will reopen the question. The problem has another cause. The folders in sdcard were created by your application?
– ramaral
Ok, it seems that I could find a way to record, using the Storage Access Framework, I will search a little more and test and if solve the problem I answer
– Lucas Batista