简体   繁体   English

Android - 获取从文件资源管理器中选择的 .txt 文件的真实路径

[英]Android - Get real path of a .txt file selected from the file explorer

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.我正在开发一个应用程序,我希望能够在 .txt 文件中从应用程序导出和导入一些数据。 The minimum API of the app is 21.该应用程序的最低 API 为 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);
    }
});

This looks like it is working.这看起来像是在工作。

But my onActivityResult doesn't work, I didn't find how I can get the file from the Uri.但是我的 onActivityResult 不起作用,我没有找到如何从 Uri 获取文件。

For now, here is my code :现在,这是我的代码:

@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.这是我在这里看到的多个帖子的混合,但似乎都不起作用。

I get this path :我得到这个路径:

/document/home:List.txt

It creates FileNotFoundException.它创建 FileNotFoundException。 How can I get the real path of the file ?如何获取文件的真实路径?

I didn't find how I can get the file from the Uri.我没有找到如何从 Uri 获取文件。

There is no file.没有文件。 ACTION_OPEN_DOCUMENT and ACTION_GET_CONTENT do not open a file. ACTION_OPEN_DOCUMENTACTION_GET_CONTENT不打开文件。 They open a document.他们打开一个文件。 That document might be a file.该文档可能是一个文件。 It might not.可能不会。 That Uri might point to:那个Uri可能指向:

  • 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数据库中BLOB列中保存的字节流
  • 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 ?如何获取文件的真实路径?

You don't.你没有。

If you wish to only accept files, integrate a file chooser library instead of using ACTION_OPEN_DOCUMENT or ACTION_GET_CONTENT .如果您只想接受文件,请集成文件选择器库,而不是使用ACTION_OPEN_DOCUMENTACTION_GET_CONTENT Just bear in mind that filesystem access to external storage is limited on Android 10+.请记住,文件系统对外部存储的访问在 Android 10+ 上受到限制。

If you use ACTION_GET_CONTENT , and the scheme of the Uri that you get is file , then getPath() will be a filesystem path.如果您使用ACTION_GET_CONTENT ,并且您获得的Uri方案是file ,则getPath()将是文件系统路径。

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.使用ContentResolveropenInputStream()将内容复制到您控制的某个文件,然后使用该文件。

Android has problem to read real path of file from device i recommended to you to use this library to select file Android在从设备读取文件的真实路径时出现问题,我建议您使用此库选择文件

implementation 'com.droidninja:filepicker:2.2.1' FielPicker 实现'com.droidninja:filepicker: 2.2.1'FielPicker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM