Search code examples
javaandroidfilepathfilenotfoundexceptionfilepicker

File Not Found after File Picked


First, I would like to apologize if this is a simplistic error. I'm mostly self taught, what I know is incredibly sporadic, and could well be haphazard.

I'm working on an app which works as a password manager. It has a backup and restore function. I got it to work using a fixed directory with no issues, but upon attempting to add a File Picker, there have been issues.

The file location is saved with no issues. Then, one line after it verifies that it was saved, it says the file doesn't exist. Here is the relevant code as well as the error log. I only will include the code up into the crash, what follows is irrelevant but can be provided if asked. PSync.psbk is the file attempting to be read. Frankly, no idea what's wrong, but I assume it's simple.

Code:

 Toast.makeText(Main.this, "Method properly called.", Toast.LENGTH_SHORT).show();
                //The file variable to imported.
                File file;

                try {
                    //Sets the file equal to the file found at the specified path.
                    //Used to access settings.
                    TinyDB database = new TinyDB(getApplicationContext());

                    String strfilePath = database.getString("FilePath");
                    Toast.makeText(Main.this, "Method properly called: " + strfilePath, Toast.LENGTH_SHORT).show();
                    file = new File(strfilePath);

                    //To be used to arrange the imported information.
                    ArrayList<String> strAcc = new ArrayList<>();
                    ArrayList<String> strUser = new ArrayList<>();
                    ArrayList<String> strPass = new ArrayList<>();
                    ArrayList<String> strAdditionalInfo = new ArrayList<>();

                    //To be used to store all the information for additional info variables. This is
                    //due to its multi-line nature requiring a slightly different method of 
                    //importation, the other variables are expected to be one line.
                    String strExtraInfo = "";

                    //Goes through the file and adds info to arrays for each corresponding variable.
                    //If the line does not have an identifier, it assumes it to be an additional
                    //info line, and will be processed later.
                    try (BufferedReader br = new BufferedReader(new FileReader(file))) {  //Line 776, as mentioned in err log.

Err log:

 W/System.err: java.io.FileNotFoundException: /document/storage/emulated/0/Download/PSync.psbk (No such file or directory)
    10-04 21:16:32.843 19405-19405/com.example.brand.psync W/System.err:     at java.io.FileInputStream.open(Native Method)
    10-04 21:16:32.843 19405-19405/com.example.brand.psync W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:146)
    10-04 21:16:32.843 19405-19405/com.example.brand.psync W/System.err:     at java.io.FileReader.<init>(FileReader.java:72)
    10-04 21:16:32.843 19405-19405/com.example.brand.psync W/System.err:     at com.example.brand.psync.Main.onRequestPermissionsResult(Main.java:776)

Solution

  • The path you are trying to access is:

    /document/storage/emulated/0/Download/PSync.psbk

    There should not be leading /document in your path. There might be a problem while storing the path.