Android - Error while saving file on SD card

Asked

Viewed 57 times

0

I recently created an application in which I share files through a Intent filter, so that the user can save the shared file to any system folder.

The purpose of this application is to share a file of Whatsapp (or any other app) and select through a file explorer the location where you want to save it.

Print aplicativo SaveFile

So far, it’s working properly for the internal storage. You can save the file to any folder within that location However, by selecting the folder where the external storage ("SD card" memory card), the system does not allow saving, and displays a message indicating permission error by issuing the following exception:

java.io.IOException: open failed: EACCES (Permission denied)
  at java.io.File.createNewFile(File.java:939)
  at br.com.luansilveira.savefile.MainActivity.btSalvarClick(MainActivity.java:370)

In the application manifest, storage permissions have already been requested:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

and when starting the application, permissions are requested for the user.

Below is the code snippet where the file is written:

public void btSalvarClick(View view) {
        String filename = diretorioAtual.getAbsolutePath() + "/" + edNomeArquivo.getText().toString();
        try {
            File novoArquivo = new File(diretorioAtual, edNomeArquivo.getText().toString());
            if (!novoArquivo.createNewFile()) {
                Toast.makeText(this, "O arquivo já existe!", Toast.LENGTH_LONG).show();
                return;
            }

            FileOutputStream outputStream = new FileOutputStream(novoArquivo);
            outputStream.write(getBytes(uri));
            outputStream.close();

            finish();
            Toast.makeText(this, "Arquivo salvo", Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(this, "Sem permissão para salvar neste local", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "Erro ao salvar: \n" + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
No answers

Browser other questions tagged

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