1

I'm trying to implement a text file picker and then put its content in an edittext, but when I try to get the file I get this error: Open Failed: ENOENT (No such file or directory)

MANIFEST

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

Asking for permissions

    public void configurar(){
      if (Build.VERSION.SDK_INT >= 23)
        {
            if (checkPermission())
            {
                // Code for above or equal 23 API Oriented Device
                // Your Permission granted already .Do next code
            } else {
                requestPermission(); // Code for permission
            }
        }
  }

   private boolean checkPermission() {
    int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
    if (result == PackageManager.PERMISSION_GRANTED) {
        return true;
    } else {
        return false;
    }
}

private void requestPermission() {

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {

    } else {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 12);
    }
}

Calling the File Picker

   public void carregarArquivo(View v) {
    Intent intent = new Intent()
            .setType("text/plain")
            .setAction(Intent.ACTION_GET_CONTENT);

        startActivityForResult(Intent.createChooser(intent, "Selecione arquivo"), Aplicacao.FILE_REQUEST_CODE);
}

Getting the callback

 @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Aplicacao.FILE_REQUEST_CODE && resultCode == RESULT_OK) {
        try {
            if(data != null){

                Uri arquivoSelecionado = data.getData();
                File arquivo = new File(arquivoSelecionado.getPath());
                StringBuilder text = new StringBuilder();
                BufferedReader br = new BufferedReader(new FileReader(arquivo));
                String line;
                while ((line = br.readLine()) != null) {
                    text.append(line);
                    text.append('\n');
                }
                br.close();
                mConteudo.setText(text);
            }

        } catch (Exception ex) {

            mConteudo.setText(ex.getMessage());

        }
    }

Additional info I'm running it on Android 5.0.1 API 21

Screenshots when I was debugging

So it openned this screen where I can choose the file to select

enter image description here

Then, after selecting that file.... enter image description here

Now the error enter image description here

The error is at this line

BufferedReader br = new BufferedReader(new FileReader(arquivo));

Does some know what I'm doing wrong here?

Joab Santos
  • 512
  • 8
  • 21
  • 1
    See also https://stackoverflow.com/q/35870825/115145 and https://stackoverflow.com/q/49221312/115145. `ACTION_GET_CONTENT` is not a file picker, in that it does not pick files and you do not get filesystem paths back. – CommonsWare Mar 30 '19 at 20:22
  • Changes and now working InputStream inputStream = getContentResolver().openInputStream(arquivoSelecionado); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); – Joab Santos Mar 30 '19 at 20:48

0 Answers0