Android - 获取从文件资源管理器中选择的.txt文件的真实路径 [英] Android - Get real path of a .txt file selected from the file explorer

查看:406
本文介绍了Android - 获取从文件资源管理器中选择的.txt文件的真实路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,我希望能够在.txt文件中从应用程序导出和导入一些数据。
该应用程序的最低API为21。

I am working on an app where I want to be able to export and import some data from the app, on a .txt file. The minimum API of the app is 21.

导出部分运行良好,但我在导入部分时遇到问题。

The export part works well, but I am having trouble with the import part.

我打开文件资源管理器:

I open the file explorer :

butImportPatient.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
                intent.setType("*/*");
                startActivityForResult(intent, IMPORTPATIENT_ACTIVITY_REQUEST_CODE);
            }
        });

这看起来有效。

但是我的onActivityResult不起作用,我没有找到如何从Uri获取文件。

But my onActivityResult doesn't work, I didn't find how I can get the file from the Uri.

现在,这是我的代码:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == IMPORTPATIENT_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
            File file = new File(data.getData().getPath()) ;
            String path = file.getAbsolutePath() ;
            StringBuilder text = new StringBuilder();

            try {
                BufferedReader br = new BufferedReader(new FileReader(path));
                String line;

                while ((line = br.readLine()) != null) {
                    text.append(line);
                    text.append("\n");

                }
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            AlertDialog.Builder builder = new AlertDialog.Builder(this) ;
            builder.setMessage(path)
                    .show() ;

        }
    }

这是多个帖子的混合我在这里看到,但似乎没有工作。

It is a mix of multiple posts I saw here, but none seems to work.

我得到这条路径:

/document/home:List.txt

它会创建FileNotFoundException。如何获得文件的真实路径?

It creates FileNotFoundException. How can I get the real path of the file ?

推荐答案


我没有找到我的方式可以从Uri获取文件。

I didn't find how I can get the file from the Uri.

没有文件。 ACTION_OPEN_DOCUMENT ACTION_GET_CONTENT 不要打开文件。他们打开一份文件。该文档可能是一个文件。它可能不会。
Uri 可能指向:

There is no file. ACTION_OPEN_DOCUMENT and ACTION_GET_CONTENT do not open a file. They open a document. That document might be a file. It might not. That Uri might point to:


  • 外部的本地文件存储

  • 其他应用的内部存储上的本地文件

  • 可移动存储上的本地文件

  • 加密并需要动态解密的本地文件

  • 数据库中 BLOB 列中保存的字节流

  • 首先需要由其他应用下载的内容

  • ...等等

  • A local file on external storage
  • A local file on internal storage for the other app
  • A local file on removable storage
  • A local file that is encrypted and needs to be decrypted on the fly
  • A stream of bytes held in a BLOB column in a database
  • A piece of content that needs to be downloaded by the other app first
  • ...and so on

如何获取文件的真实路径?

How can I get the real path of the file ?

你没有。

如果你只想接受文件,请整合文件选择器库而不是使用 ACTION_OPEN_DOCUMENT
ACTION_GET_CONTENT

If you wish to only accept files, integrate a file chooser library instead of using ACTION_OPEN_DOCUMENT or ACTION_GET_CONTENT.

如果您使用 ACTION_GET_CONTENT ,以及的方案你得到的Uri 文件,然后 getPath()将是文件系统路径。

If you use ACTION_GET_CONTENT, and the scheme of the Uri that you get is file, then getPath() will be a filesystem path.

否则,您需要了解您不知道文档的来源,并且不再考虑文件的真实路径。使用 ContentResolver openInputStream(),或 DocumentFile.fromSingleUri(),或使用 Uri 的类似技巧。

Otherwise, you need to understand that you have no idea where the document is coming from, and stop thinking in terms of "real path of the file". Use ContentResolver and openInputStream(), or DocumentFile.fromSingleUri(), or similar techniques to work with the Uri.

这篇关于Android - 获取从文件资源管理器中选择的.txt文件的真实路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆