0

This follows on from a previous post, but is a slightly different questions, hence a new post.

I am trying to open a file on an Android Tablet SD card. From this documentation, I am using the Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) to browse to a folder.

However, when I selected the folder, the uri comes backing looking like

content://com.android.externalstorage.documents/tree/3BDD-1A1D%3AMusic%2Fmidi`. 

Using URLDecoder.decode this resolves to

content://com.android.externalstorage.documents/tree/3BDD-1A1D:Music/midi`.  

Even if I select the internal storage I get something like

content://com.android.externalstorage.documents/tree/primary:Music/midi`. 

And if I use either of these paths, then appending a filename (which I know exists) to the end, I always get open failed: ENOENT (No such file or directory), no matter what I have tried with the path prefix.

My question is, how can I get a "proper formed" file path from this URI?

Update 1

I do actually want to search for files in this directory (I won't know what's there in advance in the actually working app) - trying to open a test files is just in my testing.

But even using this did not help:

 val tree = DocumentFile.fromTreeUri(this, uri)!!
 val path = tree.name

 File(path ).walk().forEach {
            val f = it
            println(it)
        }
halfer
  • 19,824
  • 17
  • 99
  • 186
peterc
  • 6,921
  • 9
  • 65
  • 131
  • You cannot just append a file name on a content scheme for a directory as you have seen. You should instead list the contents of that directory so you get uries for every sub directory and file in it. Then use such an uri. – blackapps Jun 19 '22 at 11:05
  • But why not use ACTION_OPEN_DOCUMENT to let the user pick the file? – blackapps Jun 19 '22 at 11:06
  • @blackapps I actually want to select the directory, not a specific file. I will then want to access many files within this directory – peterc Jun 20 '22 at 03:19
  • I've added a bit at [UPDATE1] in the question – peterc Jun 20 '22 at 03:32
  • `did not help` No. You cannot use the File class for a SAF uri. Try tree.listFiles(). – blackapps Jun 20 '22 at 04:38
  • And why didnt you accept the answer given at https://stackoverflow.com/questions/72590865/is-it-possible-to-get-android-sd-card-location-on-samsung-tablet-android-11 as it is to the point. – blackapps Jun 20 '22 at 04:42

0 Answers0