Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找不到文件存在文本文件的异常_Java_Android_Permissions_Intentfilter - Fatal编程技术网

Java 找不到文件存在文本文件的异常

Java 找不到文件存在文本文件的异常,java,android,permissions,intentfilter,Java,Android,Permissions,Intentfilter,我想用一个意图过滤器打开一个.txt文件,但是我得到了这个异常 W/System.err:java.io.FileNotFoundException:file:/storage/emulated/0/Download/ApplicationProposal.txt:open failed:enoint(没有这样的文件或目录) 在以下行中: FileInputStream fis = new FileInputStream(note); 例如,路径是: file:///storage/emula

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

W/System.err:java.io.FileNotFoundException:file:/storage/emulated/0/Download/ApplicationProposal.txt:open failed:enoint(没有这样的文件或目录)

在以下行中:

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);
        }
    }
}
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;
    }
它是在我的主要活动的
onCreate()
中调用的

编辑: 进一步资料:

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

File toOpen = new File(intent.getData().toString());
String text = noteHandler.loadNote(toOpen);
loadNote方法如下所示:

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);
        }
    }
}
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;
    }

您正在传递一个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://”开头,那么您可能最终会得到一个不可解析的路径
  • 即使使用格式良好的“文件:”URL,URL中的某些字符也可能已被编码;e、 g.原始路径名中的空间在URL中变为%20。操作系统可能不知道如何进行编码,您将得到一条无法解析的路径。。。再说一遍

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

不确定Android路径,但您说路径是,例如:file:///storage/emulated/0/Download/filename.txt但是异常将
文件:/
(即一个斜杠)作为前缀。我编辑了问题,我从通过intent Filter调用我的活动的intent中获取路径问题已解决:必须从路径中删除“file:///”然后回答您自己的问题:P
必须从路径中删除“file:///”。否。仅限
“文件://”