Android打开失败:ENOENT(无此类文件或目录)

时间:2019-03-30 19:57:44

标签: java android file

我正在尝试实现文本文件选择器,然后将其内容放置在edittext中,但是当我尝试获取文件时,出现以下错误:打开失败:ENOENT(没有此类文件或目录)

清单

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

寻求权限

    public void configurar(){
      if (Build.VERSION.SDK_INT >= 23)
        {
            if (checkPermission())
            {
                // Code for above or equal 23 API Oriented Device
                // Your Permission granted already .Do next code
            } else {
                requestPermission(); // Code for permission
            }
        }
  }

   private boolean checkPermission() {
    int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
    if (result == PackageManager.PERMISSION_GRANTED) {
        return true;
    } else {
        return false;
    }
}

private void requestPermission() {

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {

    } else {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 12);
    }
}

调用文件选择器

   public void carregarArquivo(View v) {
    Intent intent = new Intent()
            .setType("text/plain")
            .setAction(Intent.ACTION_GET_CONTENT);

        startActivityForResult(Intent.createChooser(intent, "Selecione arquivo"), Aplicacao.FILE_REQUEST_CODE);
}

获取回调

 @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Aplicacao.FILE_REQUEST_CODE && resultCode == RESULT_OK) {
        try {
            if(data != null){

                Uri arquivoSelecionado = data.getData();
                File arquivo = new File(arquivoSelecionado.getPath());
                StringBuilder text = new StringBuilder();
                BufferedReader br = new BufferedReader(new FileReader(arquivo));
                String line;
                while ((line = br.readLine()) != null) {
                    text.append(line);
                    text.append('\n');
                }
                br.close();
                mConteudo.setText(text);
            }

        } catch (Exception ex) {

            mConteudo.setText(ex.getMessage());

        }
    }

其他信息 我正在Android 5.0.1 API 21上运行它

调试时的屏幕截图

因此它打开了此屏幕,在这里我可以选择要选择的文件

enter image description here

然后,在选择该文件之后。...   enter image description here

出现错误   enter image description here

错误在这一行

BufferedReader br = new BufferedReader(new FileReader(arquivo));

有人知道我在做什么错吗?

0 个答案:

没有答案