Android Question Folder Picker (SD card)

james_sgp

Active Member
Licensed User
Longtime User
Hi,
My app will store a large amount of images during its use, and i want to store them on SD card (if available). So i planned to have a 'folder chooser' in the settings page of the app.
Ive looked at the ExternalStorage example, but i cant see how to return just the folder path so i can store it in KVS for the app to use?

A reason for not using internal folders as files are related to 'company liability' so the files can not be removed if the app uninstalled. Im considering cloud storage, but worried about no data connection.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can access the secondary storage without any permission using RuntimePermissions.GetSafeDirDefaultExternal, however it will not help in this case as you want to files to remain after the app is uninstalled.

In that case ExternalStorage is indeed the solution. You will let the user select a folder and pass True for UsePreviouslySelectedIfAvailable.
It is demonstrated in the example.
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
Thanks for the reply, I have tried the ExternalStorage Example; when I select a folder the app still shows "Root" at the top (see screenshot), while the log shows the actual folder path?
Log "content://com.android.externalstorage.documents/tree/primary%3ADCIM%2FCamera"

I need the path as a string so I can store the path in a KVS? Or else how can I use the path in code, to use files?
 

Attachments

  • Capture.JPG
    Capture.JPG
    22.2 KB · Views: 285
Upvote 0

agraham

Expert
Licensed User
Longtime User
Or else how can I use the path in code, to use files?
You can't use the file path directly. You need to use the methods in the ExternalStorage class to access the files. Just about all you need is there. ExternalStorage is a bit slow but you can copy the files to an internal folder if you want to manipulate them, for example by random access, with the normal File methods.

ExernalStorage saves the selected folder in a file named 'PersistantUri' by default but you can change the PreviousUriFileName field before calling SelectDir to have several different folders pre-selected. If you pass True to SelectDir it will not re-prompt the user if there is a valid URL saved in the PreviousUriFileName file.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
A further, possibly important, clarification of using ExternalStorage class which can allow access to both a folder on an SD card and also to a folder in internal 'external storage'. For internal 'external storage' the user cannot cannot directly select root, Android or Downloads when targeting SDK 30 and above on Android 11 or above but the permission dialog will tell you so and offer you the ability to create a new folder in those locations. So, for example, you cannot directly access Download or root but you can create and access a folder within both Download and root. Further, it seems from my testing, that other apps using ExternalStorage can access such folders so while you you can't share files and data between apps using Download itself you can access and share files in a folder within Download or root.

As a further aside you can check that the user has selected the expected folder by examining the Name item in the public EternalFile field 'Root' of an ExternalStorage class instance.
 
Upvote 0
Top