1

I need to user to choose a file from storage. Once user chooses the file I can't do anything with it because it comes back with an exception "File does not exist", although it most certainly does. Permissions are granted and I even take persistent URI permission. This sample code is not the prettiest, I know but it still shouldn't give me that excpetion I think. Did I write something wrong or that could cause this problem?

Currently this sample code doesn't throw an exception but if statement fails and logs "File does not exist". I need it to pass if statement and call openMap(). If I were to remove the if statement I would get org.mapsforge.map.reader.header.MapFileException: file does not exist: content:/com.android.externalstorage.documents/document/primary%3Aestonia.map

    final ActivityResultLauncher<Intent> sARL = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
    @Override
    public void onActivityResult(ActivityResult result) {
        if (result.getResultCode() == Activity.RESULT_OK){
            Intent data = result.getData();
            assert data != null;
            Uri uri = data.getData();
            File oFile = new File(uri.getPath());
            getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
            if (new File(uri.getPath()).exists()){
                openMap(oFile);
            }else{
                Log.w("File", "File does not exist");
            }

        }
    }
});

public void openFileDialog(){
    Intent data = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    data.setType("*/*");
    data = Intent.createChooser(data, "Choose tile");

    sARL.launch(data);
}
6
  • 1
    And which line throws the exception? And do you have a stack trace to share? Oct 26, 2022 at 13:35
  • FileNotFoundException is thrown. I don't have stacktrace though
    – Topstiks
    Oct 26, 2022 at 14:33
  • org.mapsforge.map.reader.header.MapFileException: file does not exist: content:/com.android.externalstorage.documents/document/primary%3Aestonia.map this is the excpetion but it comes from another library but it is not the cause.
    – Topstiks
    Oct 26, 2022 at 14:34
  • 1
    If you do not have a stack trace then do work on getting one. Among other things, that would help you answer the question I actually asked (which line throws the exception?), which should be of interest to you, too. Oct 26, 2022 at 15:11
  • I added to my original question where I got the exception. This does not help me though, no stack trace would as it would just tell me that the file does not exist but I know that it does.
    – Topstiks
    Oct 26, 2022 at 15:19

2 Answers 2

0

Try below code

            File oFile = new File(data.getData().getPath());
            if (oFile.exists()){
                Toast.makeText(this, "exist: "+oFile.exists(), Toast.LENGTH_SHORT).show(); //returns true
                openMap(oFile);
            }else{
                Log.w("File", "File does not exist");
            }

Let me know if the problem not solved yet.

1
  • 1
    This did not solve my problem sadly. I still get the same File does not exist. I tried different locations where I select the file from and even tried different file types just in case but nothing.
    – Topstiks
    Oct 27, 2022 at 11:29
0

The problem was with the Uri. If I hard code path to the file then everything is fine, but with file choosing intent I can't actually access the real file path, as far as I know. File contents can still be read but I needed the path.

File oFile = new File(Environment.getExternalStorageDirectory() + filePath)

This did the trick and I got the actual file loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.