Android Question How to use the ExternalStorage class to get a complete path

dbprogramer

Member
Licensed User
In the past I have been using FileDialog (chosendirectory= fd.FilePath &"/") to get the directory where the files are that the user wants the program to use. These could be downloaded anywhere on the device or saved on an SD card. This dialog works but is old-looking (is there a setting that changes this?) and not quite as intuitive as the ExternalStorage class looks. But I can't figure out how to get a full filepath as a string -- I'm not getting any indication of what drive has been selected.
 

dbprogramer

Member
Licensed User
I've been using the old one for years to access to the sd card. But my question is how to get the equivalent of the fd.FilePath (a string) using the ExternalStorage class.
 

dbprogramer

Member
Licensed User
No, I mean I put a directory of files on an sd card on my PC (for example), then I put it in my phone. Now my program ask where are those files. In the past I used the filedialog to let the user point to where those files are. The files could be on the sdcard or moved to somewhere on file.dirRootExternal -- I don't know where they are, that's why I need the user to show which files to open. FileDialog would give me a full path that I could use to access those files. Now I want to use ExternalStorage. But I can't use the result from the examples given to access the files -- I can't get a full path that tells me whether the files are in file.dirRootExternal or instead on the sd card (as filedialog.FilePath tells me). I don't know whether the user has saved the files on the sd card, or on the File.DirRootExternal, so I need a way to know where the user is pointing.
 

dbprogramer

Member
Licensed User
But with ExternalStorage class the user can select either the external storage (sd card) or the internal storage. How do I know which he has selected? I only get a folder name, not which "drive" is selected? Sorry to take your time, but hopefully this answer will make the ExternalStorage class more useful for everyone.
 

dbprogramer

Member
Licensed User
By analyzing (after decoding) the "persistentUri" (which I got access to by adding it to the type ExternalFile in the class), I see that if a directory in the internal memory is selected, the uri contains the word "primary" ("content://com.android.externalstorage.documents/tree/primary...") (or a few other terms such as "home" or "download") whereas the SD card has the more complex address ("content://com.android.externalstorage.documents/tree/8C34-4BBE...") -- what if I use this to at least determine which source? The rest of the address I already have (though the uri also provides the whole tree). Are there situations when this approach wouldn't be valid?

Thanks for the reference given in the previous response; it does seem to answer my question but since I only know Basic and not Java I'm at a loss as to how to implement it. I think my solution (which was inspired by trying to understand that reference) does end up providing the same info.
 

Erel

Administrator
Staff member
Licensed User
You don't need to know Java for this. Just add the code to your activity.

Are there situations when this approach wouldn't be valid?
There are zero guarantees that the URI structure will remain the same. The Storage Access Framework is an abstraction layer that provides access to many types of "files". They can come from Google Drive, a remote database a local database or any other source. Any assumption that you make on the URIs is likely to break at some point.
 
Top