【问题标题】:I am getting a FileNotFoundException but the file is there我收到 FileNotFoundException 但文件在那里
【发布时间】:2016-11-12 18:56:16
【问题描述】:

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

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

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

文件选择:

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
}

【问题讨论】:

  • 请分享你的 AndroidManifest.xml

标签: java android xml file android-intent


【解决方案1】:

ACTION_GET_CONTENT 给你一个Uri。用户通过ACTION_GET_CONTENT 选择的内容根本不必是文件,更不用说您可以访问的文件了。在这种情况下,您会得到一个带有content 方案的Uri,这很常见。

Use a ContentResolver and methods like getInputStream() 处理由该Uri 表示的内容。

【讨论】:

  • 但是我这里设置的类型选择FileXML.setDataAndType(uri, "text/xml");让用户只允许选择 xml ?!?
  • @XxGoliathusxX:用户选择的 XML 不一定是文件。即使它一个文件,也不需要ACTION_GET_CONTENT返回一个Urifile方案。
  • 好的,我阅读了你给我的链接。我正在通过查询创建内容解析器。它需要一个uri和一些我不知道的东西。你能帮我吗?
  • @XxGoliathusxX:“我正在通过查询创建内容解析器”——我不知道你的意思。您可以通过在某些Context 上调用getContentResolver() 来创建ContentResolver,例如Activity
  • 我试图遵循这 3 个步骤,这些步骤没有很好的记录: 1. 调用 openInputStream() 以获取 InputStream 以读取内容 2. 调用 getType() 以获取数据的 MIME 类型由该流支持 3. 调用 query(),请求 OpenableColumns,您可以在其中获取内容的大小以及与内容相关联的某种形式的人类可识别的“显示名称”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-13
  • 2018-05-16
  • 1970-01-01
  • 2015-02-27
  • 1970-01-01
  • 2015-02-20
  • 1970-01-01
相关资源
最近更新 更多