I'm updating an old Android app that took an xml file, parsed it and stored data into a databse. The error i get is 'no such file or directory'.
The basic fixes i tried are: -Place the file in the sdcard -Place the file in the main storage root -Use a different file explorer But the error persists.
On the programming level, i've scoured many forums and haven't found anything i might be doing wrong: -Declaring write and read permission on external storage in the manifest file. (almost every one points to this but i have these 2 permissions declared since the original version) -Checking and requesting permissions in the app, even though they're declared. (i've successfully added this bit, and tested it to be ok. Permission is granted and detected.) -Using Environment.getExternalStorageDirectory(), file.getAbsolutePath() (among others), and checking if file exists with file.exists(), it always returns an error. -Manually inputing the relative and/or absolute path, and then checking if file.exists()
In summary, the file exists, i can pick it, see it, but my app can't seem to check if it exists or read is contents to a String...
After trying everything i could think of, i'm completely out of ideas... Maybe someone can point me in the correct direction...
I download an xml file viewer which doesn't even requests external storage and it can access the file without a problem, so the issue is definitely in my app.
Some bits of my code, for reference:
// Pick the file
public void pickFile(View teste) {
// Check permissions
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 69);
} else {
// Show file picker
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, 1);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
int duratione = Toast.LENGTH_SHORT;
switch (requestCode) {
case 70:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//granted
Toast.makeText(this, "Permission granted!", duratione).show();
} else {
//not granted
Toast.makeText(this, "Permission not granted!", duratione).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
File file = new File(FilePath);
if (file.exists()) {
Toast.makeText(contexto, "EXISTE! :)", duratione).show();
return;
} else {
Toast.makeText(contexto, "Não existe... :( | "+Environment.getExternalStorageDirectory()+FilePath, duratione).show();
return;
}
// Pick the file public void pickFile(View teste) { // Show file picker Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); startActivityForResult(intent, 1); }
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (data == null) { return; } if (resultCode == RESULT_OK) { if (requestCode == 1) { String FilePath = data.getData().getPath();
I'm testing on Android 9 – user1206709 Dec 07 '20 at 14:06