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

查看:60
本文介绍了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.

我打开文件浏览器:

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_DOCUMENTACTION_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 列中的字节流
  • 需要其他应用先下载的一段内容
  • ...等等

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

How can I get the real path of the file ?

你没有.

如果您只想接受文件,请集成文件选择器库,而不是使用 <代码>ACTION_OPEN_DOCUMENT 或ACTION_GET_CONTENT.请记住,文件系统对外部存储的访问在 Android 10+ 上受到限制.

If you wish to only accept files, integrate a file chooser library instead of using ACTION_OPEN_DOCUMENT or ACTION_GET_CONTENT. Just bear in mind that filesystem access to external storage is limited on Android 10+.

如果你使用ACTION_GET_CONTENT,并且你得到的Uri的scheme是file,那么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.

否则,您需要了解您不知道文档来自哪里,并停止考虑文件的真实路径".使用 ContentResolveropenInputStream() 将内容复制到您控制的某个文件中,然后使用该文件.

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() to make a copy of the content to some file that you control, then work with that file.

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

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