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
Then, after selecting that file....
The error is at this line
BufferedReader br = new BufferedReader(new FileReader(arquivo));
Does some know what I'm doing wrong here?