【问题标题】:File not found exception for text file which exists文件未找到存在的文本文件异常
【发布时间】:2017-04-05 13:55:27
【问题描述】:

我想打开一个带有意图过滤器的 .txt 文件,但我得到了这个异常

W/System.err:java.io.FileNotFoundException:文件:/storage/emulated/0/Download/ApplicationProposal.txt:打开失败:ENOENT(没有这样的文件或目录)

在下面一行:

FileInputStream fis = new FileInputStream(note);

路径是,例如:

file:///storage/emulated/0/Download/filename.txt

对于我这样问的权限:

public void requestWritePermissions() {
    if(ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
        if(ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)){
            Toast.makeText(getApplicationContext(), "Permission needed to export Notes to SD-Card!", Toast.LENGTH_LONG);
        }
        else{
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_STORAGE);
        }
    }
}

在我的主要活动的onCreate() 中调用

编辑: 更多信息:

这就是我调用读取文件的方法

File toOpen = new File(intent.getData().toString());
String text = noteHandler.loadNote(toOpen);

loadNote 方法如下所示:

public String loadNote(File note){
        String content = "";

        try {
            FileInputStream fis = new FileInputStream(note);
            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));

            StringBuilder data = new StringBuilder();
            String line;

            do{
                line = reader.readLine();
                if (line != null)
                    data.append(line).append("\n");
            }
            while (line != null);

            content = data.toString();

            reader.close();
            fis.close();

        } catch (Exception e){
            e.printStackTrace();
            Log.e("ERROR", e.toString());
        }

        return content;
    }

【问题讨论】:

  • 不确定 Android 路径,但您说 The path is, for example: file:///storage/emulated/0/Download/filename.txt 但例外有 file:/(即一个斜线)作为前缀。
  • 我编辑了问题,我从意图中获取路径,该意图通过意图过滤器调用我的活动
  • 问题已解决:必须从路径中删除“file:///”
  • 然后回答你自己的问题:P
  • the "file:///" has to be removed from the path。没有。只有"file://"

标签: java android permissions intentfilter


【解决方案1】:

您正在传递一个 URL 字符串并尝试将其用作路径名。自然,操作系统会尝试将其解释为路径名……但它无法解析它。

假设你从一个 URL 开始,你应该做的是这样的:

    URL toOpen = new URL(intent.getData().toString());
    String text = noteHandler.loadNote(toOpen);

public String loadNote(URL note){
    String content = "";

    try {
        InputStream is = note.openStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        ....

请注意,字符串抨击“文件:” URL 有点冒险:

  • 在某些情况下,URL 可能有不同的协议。如果您假设 URL 始终以“file://”开头,那么您最终可能会得到一个不可解析的路径。
  • 即使使用格式良好的“file:” URL,URL 中的某些字符也可能已被 % 编码;例如原始路径名中的空格在 URL 中变为 %20。操作系统可能不知道如何使用 %-encoding,您将再次获得不可解析的路径。

(这些警告可能不适用于您的用例,但通常适用)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 2019-06-26
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多