【问题标题】:How to read file contents?如何读取文件内容?
【发布时间】:2018-02-19 15:42:06
【问题描述】:

我正在尝试使用以下代码从目录中选择 pdf 并读取其内容,但它不起作用

Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.setType("*/*");
        startActivityForResult(i, PICKFILE_RESULT_CODE);

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        switch(requestCode) {
            case PICKFILE_RESULT_CODE:
                if(resultCode==RESULT_OK){
//                    String filePath = data.getData().getPath();
//                    textViewFilePath.setText("File : " + filePath);
//                    readFromPdf(filePath);

                    StringBuilder text = new StringBuilder();
                    String filePath = data.getDataString();
                    try {
                        BufferedReader br = new BufferedReader(new FileReader(new File(filePath)));
                        String line;

                        while ((line = br.readLine()) != null) {
                            text.append(line);
                            text.append('n');
                        }
                        scanResults.setText(text + ".....");
                    }
                    catch (IOException e) {
                        //You'll need to add proper error handling here
                        e.printStackTrace();
                    }
                }
                break;

        }
    }

我遇到了异常

java.io.FileNotFoundException: 内容:/com.android.providers.downloads.documents/document/2295:打开 失败:ENOENT(没有这样的文件或目录)

【问题讨论】:

标签: android


【解决方案1】:

你应该像这样打开一个 InputStream

InputStream is = getContentResolver().openInputStream(data.getData());

您不应该尝试使用阅读器或尝试阅读行。

这些对于 pdf 文件没有意义。

【讨论】:

  • 您能提供完整的答案吗?我深陷其中
  • 你错过了什么?
  • 我需要获取文件路径。当前评论的一个是错误的
  • 这不是您使用的文件路径。你有一个内容方案。
  • 你还没有意识到你不能使用阅读器。并且无法从 pdf 文件中读取行?好吧,你不能。您应该想出一些不同的东西来阅读 pdf 文件的内容。你也想用线条看里面的图片吗?
猜你喜欢
  • 2012-01-29
  • 1970-01-01
  • 1970-01-01
  • 2016-03-12
  • 2015-11-08
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
相关资源
最近更新 更多