By Tonher


2018-03-11 14:51:00 8 Comments

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);
    }
});

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.

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. How can I get the real path of the file ?

2 comments

@MIF50 2019-07-04 12:11:25

Android has problem to read real path of file from device i recommended to you to use this library to select file

implementation 'com.droidninja:filepicker:2.2.1' FielPicker

@CommonsWare 2018-03-11 14:55:45

I didn't find how I can get the file from the 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:

  • 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 ?

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. Just bear in mind that filesystem access to external storage is scheduled to go away with Android Q.

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

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.

@Tonher 2018-03-11 15:13:52

Thanks for the answer ! I admit I grabbed code without really knowing what it meant. I will check that website, it seems really intersting.

@Amg91 2018-11-25 20:56:00

Which Intent should I use to get the real path? Maybe reading the file itself on the app. But getting a real file... or a real path?

@CommonsWare 2018-11-25 21:12:23

@Amg91: "Which Intent should I use to get the real path?" -- there is none. Quoting myself from my answer, "If you wish to only accept files, integrate a file chooser library".

Related Questions

Sponsored Content

22 Answered Questions

[SOLVED] How to get the filename without the extension from a path in Python?

  • 2009-03-24 16:41:03
  • Joan Venge
  • 914602 View
  • 830 Score
  • 22 Answer
  • Tags:   python string path

28 Answered Questions

[SOLVED] How to get the path of a running JAR file?

35 Answered Questions

32 Answered Questions

[SOLVED] How do I create a Java string from the contents of a file?

26 Answered Questions

[SOLVED] How to get an enum value from a string value in Java?

  • 2009-03-02 22:56:34
  • Malachi
  • 1030946 View
  • 1858 Score
  • 26 Answer
  • Tags:   java enums

21 Answered Questions

[SOLVED] Is there a way to get the source code from an APK file?

13 Answered Questions

[SOLVED] How to get an absolute file path in Python

12 Answered Questions

32 Answered Questions

[SOLVED] How to get full path of a file?

  • 2011-03-10 20:45:28
  • Jean
  • 820209 View
  • 646 Score
  • 32 Answer
  • Tags:   linux file unix path

1 Answered Questions

[SOLVED] Android: Multiple image view

  • 2018-07-25 17:50:41
  • Sarfaraz
  • 354 View
  • 0 Score
  • 1 Answer
  • Tags:   java android

Sponsored Content