Java 我收到一个FileNotFoundException,但文件在那里

Java 我收到一个FileNotFoundException,但文件在那里,java,android,xml,file,android-intent,Java,Android,Xml,File,Android Intent,当我试图打开通过意图选择的文件时,会出现以下错误: java.io.FileNotFoundException: /document/primary:Android/data/com.oli.myapp/Files/test.xml:open 失败:enoint(没有这样的文件或目录) 我不知道为什么会这样。该文件存在,因为我选择了它。这是我的密码: 文件选择: Intent chooseFileXML = new Intent(Intent.ACTION_GET_CONTENT); Uri u

当我试图打开通过意图选择的文件时,会出现以下错误:

java.io.FileNotFoundException: /document/primary:Android/data/com.oli.myapp/Files/test.xml:open 失败:enoint(没有这样的文件或目录)

我不知道为什么会这样。该文件存在,因为我选择了它。这是我的密码:

文件选择:

Intent chooseFileXML = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(new Helper(FunctionsActivity.this).getPathToAppFolder());                
chooseFileXML.setDataAndType(uri, "text/xml");
Intent intentXML = Intent.createChooser(chooseFileXML, getString(R.string.importXMLDatei));
startActivityForResult(intentXML, REQUEST_CODE_IMPORT_XML_FILE);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    switch (requestCode){
        case REQUEST_CODE_IMPORT_XML_FILE:
            if(resultCode == RESULT_OK){

                String Fpath = data.getDataString();
                File file = new File(Fpath);
                Intent intent = new Intent(FunctionsActivity.this, CreateActivity.class);
                intent.setAction(Intent.ACTION_DEFAULT);
                intent.setData(Uri.parse(file.toURI().toString()));
                startActivity(intent);


            }
            break;
    }
}
Uri uri = data.getData();
DocumentFile documentFile = DocumentFile.fromSingleUri(this, uri);
String type = documentFile.getType();
if(type.equals("text/xml")){
    try {
        InputStream inputStream = getContentResolver().openInputStream(uri);
        if(inputStream == null){
            throw new Exception();
        }
        BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder total = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line).append('\n');
        }

        //Could read the file with no problems
        createWithXMLCode(total.toString());

    }catch (Exception e){
        e.printStackTrace();
        //TODO

    }
}else{
    //TODO
}
获取它的代码:

Intent chooseFileXML = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(new Helper(FunctionsActivity.this).getPathToAppFolder());                
chooseFileXML.setDataAndType(uri, "text/xml");
Intent intentXML = Intent.createChooser(chooseFileXML, getString(R.string.importXMLDatei));
startActivityForResult(intentXML, REQUEST_CODE_IMPORT_XML_FILE);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    switch (requestCode){
        case REQUEST_CODE_IMPORT_XML_FILE:
            if(resultCode == RESULT_OK){

                String Fpath = data.getDataString();
                File file = new File(Fpath);
                Intent intent = new Intent(FunctionsActivity.this, CreateActivity.class);
                intent.setAction(Intent.ACTION_DEFAULT);
                intent.setData(Uri.parse(file.toURI().toString()));
                startActivity(intent);


            }
            break;
    }
}
Uri uri = data.getData();
DocumentFile documentFile = DocumentFile.fromSingleUri(this, uri);
String type = documentFile.getType();
if(type.equals("text/xml")){
    try {
        InputStream inputStream = getContentResolver().openInputStream(uri);
        if(inputStream == null){
            throw new Exception();
        }
        BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder total = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line).append('\n');
        }

        //Could read the file with no problems
        createWithXMLCode(total.toString());

    }catch (Exception e){
        e.printStackTrace();
        //TODO

    }
}else{
    //TODO
}
编辑:

Intent chooseFileXML = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(new Helper(FunctionsActivity.this).getPathToAppFolder());                
chooseFileXML.setDataAndType(uri, "text/xml");
Intent intentXML = Intent.createChooser(chooseFileXML, getString(R.string.importXMLDatei));
startActivityForResult(intentXML, REQUEST_CODE_IMPORT_XML_FILE);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    switch (requestCode){
        case REQUEST_CODE_IMPORT_XML_FILE:
            if(resultCode == RESULT_OK){

                String Fpath = data.getDataString();
                File file = new File(Fpath);
                Intent intent = new Intent(FunctionsActivity.this, CreateActivity.class);
                intent.setAction(Intent.ACTION_DEFAULT);
                intent.setData(Uri.parse(file.toURI().toString()));
                startActivity(intent);


            }
            break;
    }
}
Uri uri = data.getData();
DocumentFile documentFile = DocumentFile.fromSingleUri(this, uri);
String type = documentFile.getType();
if(type.equals("text/xml")){
    try {
        InputStream inputStream = getContentResolver().openInputStream(uri);
        if(inputStream == null){
            throw new Exception();
        }
        BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder total = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line).append('\n');
        }

        //Could read the file with no problems
        createWithXMLCode(total.toString());

    }catch (Exception e){
        e.printStackTrace();
        //TODO

    }
}else{
    //TODO
}

ACTION\u GET\u CONTENT
为您提供一个
Uri
。用户通过
ACTION\u GET\u CONTENT
选择的内容根本不必是文件,更不用说您可以访问的文件了。在本例中,您将获得一个
Uri
以及一个
内容
方案,这是非常常见的


要使用该
Uri

表示的内容,请共享您的AndroidManifest.xml,但我在此处设置的类型选择FileXML.setDataAndType(Uri,“text/xml”);让用户只允许选择xml@XxGoliathusxX:用户选择的XML不必是文件。即使它是一个文件,也不要求
ACTION\u GET\u CONTENT
返回带有
文件
方案的
Uri
。好的,我读了你给我的链接。我正在通过查询创建contentresolver。它需要一个uri和一些我不知道的东西。你能帮我吗?@XxGoliathusxX:“我正在通过查询创建一个contentresolver”--我不知道你的意思。通过在一些
上下文
上调用
getContentResolver()
,例如
活动
,您可以创建一个
ContentResolver。调用openInputStream()获取要在内容2中读取的InputStream。调用getType()获取该流支持的数据的MIME类型3。调用query(),询问OpenableColumns,从中可以获得内容的大小以及与内容相关联的某种形式的人类可识别的“显示名称”