【问题标题】:File Not Found after File Picked选择文件后未找到文件
【发布时间】:2017-10-05 05:27:01
【问题描述】:

首先,如果这是一个简单的错误,我想道歉。我主要是自学成才,我所知道的非常零星,而且很可能是偶然的。

我正在开发一个用作密码管理器的应用程序。它具有备份和恢复功能。我使用固定目录让它工作没有问题,但是在尝试添加文件选择器时,出现了问题。

文件位置保存没有问题。然后,在它验证它已保存后的一行,它说该文件不存在。这是相关代码以及错误日志。我只会将代码包含在崩溃中,接下来的内容无关紧要,但如果被要求可以提供。 PSync.psbk 是试图读取的文件。坦率地说,不知道出了什么问题,但我认为这很简单。

代码:

 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.

错误日志:

 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)

【问题讨论】:

  • 您是否在清单中授予了权限
  • 是的,我有,它也要求运行时权限。当文件格式为 file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "PSync.psbk"); 时读取文件没有问题;
  • @NabinBhandari,是的,这段代码 sn-p 在 onRequestPermissionsResult 中。代码仅在授予权限后运行。正如我告诉 Amrutha 的,当我使用 Environmemnt.get 方法时,从同一目录读取同一文件没有问题

标签: java android filepath filenotfoundexception filepicker


【解决方案1】:

您尝试访问的路径是:

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

路径中不应有前导 /document。存储路径时可能会出现问题。

【讨论】:

  • 只是在我测试之前澄清一下,路径应该是什么样的? /storage/emulated/0/Download/PSync.psbk ?
  • 你这么说之后,我决定测试一些东西,因为我觉得它很奇怪。我通过广播意图并让他们使用文件管理器来获取文件路径。出于某种奇怪的原因,我的主文件浏览器将其返回为“/document/storage/emulated/0/Download/PSync.psbk”,但使用不同的文件浏览器返回了正确的路径并允许它工作。由于这不是代码中的错误,我应该如何处理回答这个问题?只是检查你的对吗?还是因为和代码无关就删掉?
  • 我找到了错误所在。我认为将我的答案标记为已接受并没有什么坏处。 ;)
  • 很公平哈哈谢谢,顺便说一句。从来没想过要尝试另一个文件浏览器,我需要睡觉。
猜你喜欢
  • 1970-01-01
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 2019-10-08
  • 2013-04-06
相关资源
最近更新 更多