【问题标题】:Android: reading from text file - finished with exception open failed: ENOENT (No such file or directory)Android:从文本文件读取 - 完成异常打开失败:ENOENT(没有这样的文件或目录)
【发布时间】:2020-08-01 17:44:19
【问题描述】:

我是 Android 应用程序开发 (java) 方面的新手,我需要帮助。 我需要读取文本文件并将每一行存储到 List。 我在stackoverflow中阅读了很多讨论。其中之一启发了我的代码。但我无法解决我的问题...

所以我像下面这样调用文件选择器:

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);

try {                                
    startActivityForResult(Intent.createChooser(intent, getString(R.string.vyber_soubor)), VYBER_SOUBORU_POZNAMKA);
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(kontext, "SeznamPoznamek::onCreate::tlPridej.setOnClickListener - Please install a File Manager.", Toast.LENGTH_LONG).show();
}

在 onActivityResult 函数中(下面只显示必要的部分,函数的其余部分标记为点)从所选文件的意图 uri 中选择。使用函数 getPath 从 uri 文件路径中提取为字符串。这个文件路径字符串我发送到函数“importujTextovouPoznamku”,我想从文本文件中读取行。 但在“FileInputStream fis = new FileInputStream(mujExterniSoubor);”这一行是例外 ("java.io.FileNotFoundException: /document/primary:DCIM/veci_svor.txt: open failed: ENOENT (No such file or directory)") throw

在清单中是:

并且应用程序有权写入存储。

我很惊讶我的文件路径中有双点(您可以在异常中看到我的文件路径字符串,还可以看到我的文件存储在 DCIM 文件夹中)

对我来说第二个惊喜是,在真机华为 Honor 6A (android 6) 上,此代码有效(但仅在此手机上),但在摩托罗拉 e5 plus(android 8)或虚拟机(android 6 或8).

请任何人帮助我或建议我应该在我的代码中继续尝试什么,无一例外 谢谢fik236

部分代码:

    private String getPath(Uri uri) {
        String path = null;
        String[] projection = { MediaStore.Files.FileColumns.DATA };
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);

        if(cursor == null){
            path = uri.getPath();
        }
        else{
            cursor.moveToFirst();
            int column_index = cursor.getColumnIndexOrThrow(projection[0]);
            path = cursor.getString(column_index);
            cursor.close();
        }

        return ((path == null || path.isEmpty()) ? (uri.getPath()) : path);
    }

public void onActivityResult(int kodVolani, int vysledekVolani, Intent mujVraceciIntent) {
        super.onActivityResult(kodVolani, vysledekVolani, mujVraceciIntent);
    .
    .
    .
    } else if ( kodVolani== VYBER_SOUBORU_POZNAMKA) {
            Uri uri = mujVraceciIntent.getData();
            String src = getPath(uri);
            importujTextovouPoznamku(src);
    }
    .
    .
    .


public void importujTextovouPoznamku (String jmenoSouboru) {
    List<String> precteneRadky = new ArrayList<>();

        File mujExterniSoubor = new File(jmenoSouboru);
        try {
            FileInputStream fis = new FileInputStream(mujExterniSoubor); //problem
            DataInputStream in = new DataInputStream(fis);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            String radek;
            while ((radek = br.readLine()) != null) {
                precteneRadky.add(radek);
            }
            br.close();
            in.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(getBaseContext(),"SeznamPoznamek.java::prectiRadkyZeSouboru:" + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    .
    .
    .

【问题讨论】:

  • document/primary:DCIM/veci_svor.txt 那不是有效的文件系统路径。

标签: java android


【解决方案1】:

不要试图从 uri 获取路径以打开该路径的文件输入流。最好直接为 uri 打开一个输入流。

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

然后从你的文件中读取。

【讨论】:

  • 太棒了!这有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多