0

I am trying to open and then move a file. I am choosing a file using the file picker via an intent, this returns a result which is a Uri pointing to the file on the internal storage of the device.

When I take a look at the path of the Uri it is as follow, /document/856 which represents the file which I choose, which is TextFile named README.txt. I am trying to make use of the Uri to read this file into a File object, which I then want to move to /data/user/0/com.example.bootloader/files/bluetoothchat_files/ which is an internal storage directory of my app.

At the moment I can't seem to figure out how to use the Uri which is returned in the onActivityResult call back to open a File object. I a trying the file, but when I try to check the "file it gives file not found".

I have tried most to all of the stackoverflow solutions on this topic but can't seem to solve my problem. I have read https://developer.android.com/training/data-storage/files.html#WriteInternalStorage , but I want use a Uri to open the file.

This is the onActivity result callback code to check if the file exists.

           if (resultCode == Activity.RESULT_OK) {

                Uri uploadfileuri = data.getData();
                File file = new File(uploadfileuri.getPath());


                if(file.exists())
                {
                    Log.i(TAG, "onActivityResult: Found the file");
                }
                else
                {
                    Log.i(TAG, "onActivityResult: Cannot find the file");
                }

            }

This line is used to trigger the intent for the file picker.

startActivityForResult(newIntent(Intent.ACTION_GET_CONTENT).setType("*/"),CHOOSE_FILE);

I am expecting to be able to open the file which is returned by the Uri making use of a File object. If I can open the File I am planning to use the srcFile.to(desFile) approach to move my file to my desired directory. Any help or ideas on how I can solve the problem of opening the desired file from the Uri??

Johan Fick
  • 375
  • 2
  • 14
  • 1
    See also https://stackoverflow.com/questions/48510584/onactivityresults-intent-getpath-doesnt-give-me-the-correct-filename and https://stackoverflow.com/questions/35870825/getting-the-absolute-file-path-from-content-uri-for-searched-images. "I am expecting to be able to open the file which is returned by the Uri making use of a File object" -- that is not how this works. "Any help or ideas on how I can solve the problem of opening the desired file from the Uri?" -- it's not a file. Use `openInputStream()` on `ContentResolver` to get an `InputStream` on the content identified by the `Uri`. – CommonsWare May 30 '19 at 13:17
  • Thank you very much!! I found the file using your suggestion of InputStream, saved my day. I have been stuck on this for the entire day. – Johan Fick May 30 '19 at 13:29

0 Answers0