0

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;
}
  • 1
    `ACTION_GET_CONTENT` has nothing to do with files, and `checkSelfPermission()` and `requestPermissions()` have nothing to do with `ACTION_GET_CONTENT`. Where is `FilePath` coming from? What version of Android are you testing on? – CommonsWare Dec 04 '20 at 18:40
  • FilePath: // 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
  • `String FilePath = data.getData().getPath();` -- that will not work on modern versions of Android. – CommonsWare Dec 07 '20 at 14:13
  • Interestingly, it 'works' (by that, i mean, it does not throw an erro), and i get a relative path to the file, which i can output... Do you know what should i use instead? – user1206709 Dec 07 '20 at 14:41
  • "and i get a relative path to the file" -- no, you do not. For example, there is no requirement that the user choose a file via `ACTION_GET_CONTENT`. "Do you know what should i use instead?" -- please see the duplicate questions, in the blue box at the top of the page. – CommonsWare Dec 07 '20 at 14:49
  • 1
    Got it! I can't get access to the file, only get a copy of it's content... Here's the code: Uri uri = data.getData(); InputStreamReader inputStreamReader = new InputStreamReader(getContentResolver().openInputStream(uri)); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder sb = new StringBuilder(); String s; while ((s = bufferedReader.readLine()) != null) { sb.append(s); } String fileContent = sb.toString(); A copy of the file content gets stored in fileContent. Thanks for pointing in the right direction! – user1206709 Dec 07 '20 at 16:26

0 Answers0