B4A Class ExternalStorage - Access SD cards and USB sticks

Discussion in 'Additional libraries, classes and official updates' started by Erel, Mar 5, 2018.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    [​IMG]

    Before we start:

    1. External storage means a real sd card or a connected mass storage USB device.
    2. It has nothing to do with File.DirRootExternal / DirDefaultExternal which actually point to an internal storage.
    3. It has nothing to do with runtime permissions.
    4. You can use RuntimePermissions.GetAllSafeDirsExternal to directly access a specific folder on the SD card.
    5. The minimum version for this class is Android 5. It might work with Android 4.4 (change minSdkVersion if you like to try it).

    Starting from Android 4.4 it is no longer possible to directly access external storages.
    The only way to access these storages is through the Storage Access Framework (SAF), which is a quite complex and under-documented framework.

    The ExternalStorage class makes it simpler to work with SAF.

    Usage:

    1. Call ExternalStorage.SelectDir. This will open a dialog that will allow the user to select the root folder. Once selected the uri of the root folder is stored and can be later used without requiring the user to select the folder again. Even after the device is booted.

    2. Wait For the ExternalFolderAvailable event.
    Now you can access the files under Storage.Root, including inside subfolders.

    3. Files are represented as a custom type named ExternalFile.

    4. The following operations are supported: ListFiles, Delete, CreateNewFile, FindFile, OpenInputStream and OpenOutputStream.

    See the attached example.

    Depends on: ContentResolver and JavaObject libraries.
    Add:
    Code:
    #AdditionalJar: com.android.support:support-core-utils
    Updates

    - V1.02: Fix for issue related to the conversion of URIs to strings.
    - V1.01: Fixes an issue with Storage.ListFiles. Credit goes to @agraham !
     

    Attached Files:

    Last edited: Jan 27, 2019
    fredo, anOparator, asales and 43 others like this.
  2. Claudio Oliveira

    Claudio Oliveira Active Member Licensed User

    @Erel,

    I'm trying your sample project "as is", and I'm getting this error.
    Did I miss something?

     
  3. Erel

    Erel Administrator Staff Member Licensed User

    You should use a newer version of android.jar. Make sure to update it with B4A Sdk Manager and then configure it under Tools - Configure Paths.
     
    Claudio Oliveira likes this.
  4. Claudio Oliveira

    Claudio Oliveira Active Member Licensed User

    Brilliant!
    Thanks!
     
  5. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    The posted project has a lot of files.
    Would it be possible to condense all that in a library or add it to the Files library?

    RBS
     
  6. DonManfred

    DonManfred Expert Licensed User

    You are talking about WHICH FILES exactly? There is only ONE Classfile used.....
    Additionally you should have started a NEW THREAD for your question.
     
  7. Meigionel TS

    Meigionel TS Member Licensed User

    How would you detect programmatically if a usb mass storage is attached to the device? Consider this scenario, I have used
    Code:
    storage.SelectDir(True)
    where the directory I selected was of a usb mass storage, then I removed the mass storage, and try
    Code:
    Wait For Storage_ExternalFolderAvailable
        storage.CreateNewFile(storage.Root,
    "t" & Rnd(1,55555) & ".txt")
    instead of throwing a new dialog to select a new path (because the old path no longer exists), it still uses the old path and does not throw any error.

    This is a reason why I am looking for a way to detect if the usb mass storage is still there and if it is then use the previous path else ask user to select a new directory again.
     
  8. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    > You are talking about WHICH FILES exactly?

    All the files in the posted file ExternalStorage.zip.
    As they were in the .zip I presumed they were somehow used.

    RBS
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    You only need to add the ExternalStorage class to your code.

    What is the output of CreateNewFile?

    What happens if you try to list the files after the USB was removed?
     
  10. Meigionel TS

    Meigionel TS Member Licensed User

    The program runs without throwing any error but since the path does not exists, no file is created anywhere.

    I get an null pointer exception (app crashes), and that is what I need to solve. If the user removes the usb storage, I need to detect the change and show a new dialog to select the path again. But I could not understand how to. :/
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    Catch the error with Try / Catch and know that there is no USB.
     
    Meigionel TS likes this.
  12. Erel

    Erel Administrator Staff Member Licensed User

    Example updated with a fix to the issue where the wrong image was shown. Thank you @Geezer!
     
  13. boon2

    boon2 Member Licensed User

    upload_2018-9-1_13-22-1.png
    error
    #AdditionalJar: com.android.support:support-core-utils
    1.i want link to download .jar
    2.and save in where is folder
    thanks.
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    Please post errors as text instead of a screenshot.
    There is no such thing.

    You need to use B4A Sdk Manager. This resource is probably included in the recommended items list. If not then search for support-core-utils and install it.
     
  15. boon2

    boon2 Member Licensed User

    Thanks.
     

    Attached Files:

  16. boon2

    boon2 Member Licensed User

    can't intall to samsung s3 android 4.3
    sdk version 28
    upload_2018-9-3_13-51-43.png
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    Please post the error message as text instead of screenshot.
     
  18. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    This message is because you had installed it on your cell phone, you must remove the previous installation and then try again or simply change the name of the package (Ctrl + B).
     
    boon2 likes this.
  19. Erel

    Erel Administrator Staff Member Licensed User

    - V1.01: Fixes an issue with Storage.ListFiles. Credit goes to @agraham !

    - V1.02 was also released with another bug fix.
     
    Last edited: Jan 27, 2019
    johndb, Johan Hormaza and DonManfred like this.
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice