B4A Library Storage v1.0

DonManfred

Expert
Licensed User
This library is a wrapper for the new SDK-Api in Android 5 (Lollipop).
http://developer.android.com/reference/android/os/Environment.html

ATTENTION: YOU NEED ANDROID 5+ (Lollipop) to use this library.

Storage
Author:
DonManfred
Version: 1
  • env
    Methods:
    • Initialize As Map
      Initialize the Environment and return a Map of Paths
      Parameter
      String EventName
      The Eventname to use to raise events
      Return type: @return:
    • getExternalStoragePublicDirectory (type As String) As String
      Get a top-level public external storage directory for placing files of a particular type. This
      is where the user will typically place and manage their own files, so you should be careful about
      what you put here to ensure you don't erase their files or get in the way of their own organization.
      On devices with multiple users (as described by UserManager), each user has their own isolated
      external storage. Applications only have access to the external storage for the user they're
      running as.
      type The type of storage directory to return. Should be one of DIRECTORY_MUSIC,
      DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS,
      DIRECTORY_PICTURES, DIRECTORY_MOVIES, DIRECTORY_DOWNLOADS, or DIRECTORY_DCIM.
      May not be null.
      Returns the File path for the directory. Note that this directory may not yet exist, so you must
      make sure it exists before using it such as with File.mkdirs().
    • getExternalStorageState (path As File) As String
      Returns the current state of the storage device that provides the given path.
      Returns one of MEDIA_UNKNOWN, MEDIA_REMOVED, MEDIA_UNMOUNTED, MEDIA_CHECKING,
      MEDIA_NOFS, MEDIA_MOUNTED, MEDIA_MOUNTED_READ_ONLY, MEDIA_SHARED, MEDIA_BAD_REMOVAL
      or MEDIA_UNMOUNTABLE.
    • isExternalStorageEmulated As Boolean
      Returns whether the primary "external" storage device is emulated.
      If true, data stored on this device will be stored on a portion of
      the internal storage system.
    • isExternalStorageRemovable (path As String) As Boolean
      Returns whether the storage device that provides the given path is removable.
      Returns true if the storage device can be removed (such as an SD card), or
      false if the storage device is built in and cannot be physically removed.
    Properties:
    • DataDirectory As String [read only]
      Return the user data directory.
    • DownloadCacheDirectory As String [read only]
      Return the download/cache content directory.
    • ExternalStorageDirectory As String [read only]
      Return the primary external storage directory. This directory may not currently be accessible if
      it has been mounted by the user on their computer, has been removed from the device, or some other
      problem has happened. You can determine its current state with getExternalStorageState().
      Note: don't be confused by the word "external" here. This directory can better be thought as
      media/shared storage. It is a filesystem that can hold a relatively large amount of data and that
      is shared across all applications (does not enforce permissions). Traditionally this is an SD
      card, but it may also be implemented as built-in storage in a device that is distinct from the
      protected internal storage and can be mounted as a filesystem on a computer.
      On devices with multiple users (as described by UserManager), each user has their own isolated
      external storage. Applications only have access to the external storage for the user they're
      running as.
      In devices with multiple "external" storage directories, this directory represents the "primary"
      external storage that the user will interact with. Access to secondary storage is available through
      Applications should not directly use this top-level directory, in order to avoid polluting the
      user's root namespace. Any files that are private to the application should be placed in a
      directory returned by Context.getExternalFilesDir, which the system will take care of deleting
      if the application is uninstalled. Other shared files should be placed in one of the directories
      returned by getExternalStoragePublicDirectory(String).
      Writing to this path requires the WRITE_EXTERNAL_STORAGE permission, and starting in read
      access requires the READ_EXTERNAL_STORAGE permission, which is automatically granted if
      you hold the write permission.
      Starting in KITKAT, if your application only needs to store internal data, consider using
      getExternalFilesDir(String) or getExternalCacheDir(), which require no permissions to read
      or write.
      This path may change between platform versions, so applications should only persist relative paths.
    • ExternalStorageState As String [read only]
      Returns the current state of the primary "external" storage device. Returns one of
      MEDIA_UNKNOWN, MEDIA_REMOVED, MEDIA_UNMOUNTED, MEDIA_CHECKING, MEDIA_NOFS,
      MEDIA_MOUNTED, MEDIA_MOUNTED_READ_ONLY, MEDIA_SHARED, MEDIA_BAD_REMOVAL or
      MEDIA_UNMOUNTABLE.
    • Paths As Map [read only]
      Get a Map of systempaths.
    • RootDirectory As String [read only]
      Return root of the "system" partition holding the core Android OS.
      Always present and mounted read-only.

The Example is based on Margrets example to get the mount point.
The library is used to determine more informations about the mountpoint to hopefully find the external sdcard-path
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim storage As env
    Dim paths As Map
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    paths = storage.Initialize
    For i = 0 To paths.Size-1
        Log(paths.GetKeyAt(i)&"="&paths.GetValueAt(i))
    Next

    Dim de As String = File.DirRootExternal
    Log ("DirRootExternal = "&de)
    Dim mtc As Matcher = Regex.Matcher("(/|\\)[^(/|\\)]*(/|\\)",de)
    Dim extsdcard As String = de
    If mtc.Find = True Then
        Dim mnt As String = mtc.Group(0)
   
        Log ("mount point = "& mnt)
        Dim dirs As List = File.ListFiles(mnt)
        For Each f As String In dirs
            If storage.isExternalStorageRemovable(mnt&f) Then
                Log ("Device = "& f&":"&mnt&f&" is removable")
                If File.ListFiles(mnt&f).IsInitialized Then
                    Log("probably ExtSDCard: "&mnt&f)
                    extsdcard = mnt&f
                Else
                    'Log("Problem reading "&mnt&f)
                End If
            Else
                Log ("Device = "& f&":"&mnt&f&" is NOT removable")
            End If
        Next
    End If
    Log("extsdcard probably="&extsdcard)
    For Each f As String In File.ListFiles(extsdcard)
        Log(">"&f)
    Next

End Sub
** Activity (main) Create, isFirst = true **
DIRECTORY_ALARMS=Alarms
DIRECTORY_DCIM=DCIM
DIRECTORY_DOCUMENTS=Documents
DIRECTORY_DOWNLOADS=Download
DIRECTORY_MOVIES=Movies
DIRECTORY_MUSIC=Music
DIRECTORY_NOTIFICATIONS=Notifications
DIRECTORY_PICTURES=Pictures
DIRECTORY_PODCASTS=Podcasts
DIRECTORY_RINGTONES=Ringtones
DirRootExternal = /storage/emulated/0
mount point = /storage/
Device = usbdisk:/storage/usbdisk is removable
Device = sdcard1:/storage/sdcard1 is removable
probably ExtSDCard: /storage/sdcard1
Device = sdcard0:/storage/sdcard0 is NOT removable
Device = emulated:/storage/emulated is NOT removable
extsdcard=/storage/sdcard1
>I am a FOLDER on the External SDCard
>I am a file on the External SDCard.txt
>LOST.DIR
>.android_secure
>Android
>DCIM
>carbon
>data
** Activity (main) Resume **
 

Attachments

Last edited:

koaunglay

Member
Licensed User
This library is a wrapper for the new SDK-Api in Android 5 (Lollipop).
http://developer.android.com/reference/android/os/Environment.html

ATTENTION: YOU NEED ANDROID 5+ (Lollipop) to use this library.

Storage
Author:
Manfred Ssykor (msy1912@gmail.com)
Version: 1
  • env
    Methods:
    • Initialize As Map
      Initialize the Environment and return a Map of Paths
      Parameter
      String EventName
      The Eventname to use to raise events
      Return type: @return:
    • getExternalStoragePublicDirectory (type As String) As String
      Get a top-level public external storage directory for placing files of a particular type. This
      is where the user will typically place and manage their own files, so you should be careful about
      what you put here to ensure you don't erase their files or get in the way of their own organization.
      On devices with multiple users (as described by UserManager), each user has their own isolated
      external storage. Applications only have access to the external storage for the user they're
      running as.
      type The type of storage directory to return. Should be one of DIRECTORY_MUSIC,
      DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS,
      DIRECTORY_PICTURES, DIRECTORY_MOVIES, DIRECTORY_DOWNLOADS, or DIRECTORY_DCIM.
      May not be null.
      Returns the File path for the directory. Note that this directory may not yet exist, so you must
      make sure it exists before using it such as with File.mkdirs().
    • getExternalStorageState (path As File) As String
      Returns the current state of the storage device that provides the given path.
      Returns one of MEDIA_UNKNOWN, MEDIA_REMOVED, MEDIA_UNMOUNTED, MEDIA_CHECKING,
      MEDIA_NOFS, MEDIA_MOUNTED, MEDIA_MOUNTED_READ_ONLY, MEDIA_SHARED, MEDIA_BAD_REMOVAL
      or MEDIA_UNMOUNTABLE.
    • isExternalStorageEmulated As Boolean
      Returns whether the primary "external" storage device is emulated.
      If true, data stored on this device will be stored on a portion of
      the internal storage system.
    • isExternalStorageRemovable (path As String) As Boolean
      Returns whether the storage device that provides the given path is removable.
      Returns true if the storage device can be removed (such as an SD card), or
      false if the storage device is built in and cannot be physically removed.
    Properties:
    • DataDirectory As String [read only]
      Return the user data directory.
    • DownloadCacheDirectory As String [read only]
      Return the download/cache content directory.
    • ExternalStorageDirectory As String [read only]
      Return the primary external storage directory. This directory may not currently be accessible if
      it has been mounted by the user on their computer, has been removed from the device, or some other
      problem has happened. You can determine its current state with getExternalStorageState().
      Note: don't be confused by the word "external" here. This directory can better be thought as
      media/shared storage. It is a filesystem that can hold a relatively large amount of data and that
      is shared across all applications (does not enforce permissions). Traditionally this is an SD
      card, but it may also be implemented as built-in storage in a device that is distinct from the
      protected internal storage and can be mounted as a filesystem on a computer.
      On devices with multiple users (as described by UserManager), each user has their own isolated
      external storage. Applications only have access to the external storage for the user they're
      running as.
      In devices with multiple "external" storage directories, this directory represents the "primary"
      external storage that the user will interact with. Access to secondary storage is available through
      Applications should not directly use this top-level directory, in order to avoid polluting the
      user's root namespace. Any files that are private to the application should be placed in a
      directory returned by Context.getExternalFilesDir, which the system will take care of deleting
      if the application is uninstalled. Other shared files should be placed in one of the directories
      returned by getExternalStoragePublicDirectory(String).
      Writing to this path requires the WRITE_EXTERNAL_STORAGE permission, and starting in read
      access requires the READ_EXTERNAL_STORAGE permission, which is automatically granted if
      you hold the write permission.
      Starting in KITKAT, if your application only needs to store internal data, consider using
      getExternalFilesDir(String) or getExternalCacheDir(), which require no permissions to read
      or write.
      This path may change between platform versions, so applications should only persist relative paths.
    • ExternalStorageState As String [read only]
      Returns the current state of the primary "external" storage device. Returns one of
      MEDIA_UNKNOWN, MEDIA_REMOVED, MEDIA_UNMOUNTED, MEDIA_CHECKING, MEDIA_NOFS,
      MEDIA_MOUNTED, MEDIA_MOUNTED_READ_ONLY, MEDIA_SHARED, MEDIA_BAD_REMOVAL or
      MEDIA_UNMOUNTABLE.
    • Paths As Map [read only]
      Get a Map of systempaths.
    • RootDirectory As String [read only]
      Return root of the "system" partition holding the core Android OS.
      Always present and mounted read-only.

The Example is based on Margrets example to get the mount point.
The library is used to determine more informations about the mountpoint to hopefully find the external sdcard-path
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim storage As env
    Dim paths As Map
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    paths = storage.Initialize
    For i = 0 To paths.Size-1
        Log(paths.GetKeyAt(i)&"="&paths.GetValueAt(i))
    Next

    Dim de As String = File.DirRootExternal
    Log ("DirRootExternal = "&de)
    Dim mtc As Matcher = Regex.Matcher("(/|\\)[^(/|\\)]*(/|\\)",de)
    Dim extsdcard As String = de
    If mtc.Find = True Then
        Dim mnt As String = mtc.Group(0)
   
        Log ("mount point = "& mnt)
        Dim dirs As List = File.ListFiles(mnt)
        For Each f As String In dirs
            If storage.isExternalStorageRemovable(mnt&f) Then
                Log ("Device = "& f&":"&mnt&f&" is removable")
                If File.ListFiles(mnt&f).IsInitialized Then
                    Log("probably ExtSDCard: "&mnt&f)
                    extsdcard = mnt&f
                Else
                    'Log("Problem reading "&mnt&f)
                End If
            Else
                Log ("Device = "& f&":"&mnt&f&" is NOT removable")
            End If
        Next
    End If
    Log("extsdcard probably="&extsdcard)
    For Each f As String In File.ListFiles(extsdcard)
        Log(">"&f)
    Next

End Sub
What is my error? Please!
B4X:
** Activity (main) Create, isFirst = true **
java.lang.NoSuchFieldError: android.os.Environment.DIRECTORY_DOCUMENTS
    at de.donmanfred.storage.getPaths(storage.java:45)
    at de.donmanfred.storage.Initialize(storage.java:34)
    at b4a.example.main._activity_create(main.java:327)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at b4a.example.main.afterFirstLayout(main.java:100)
    at b4a.example.main.access$100(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:78)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5457)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
    at dalvik.system.NativeStart.main(Native Method)
 

koaunglay

Member
Licensed User
What is my error? Please!
B4X:
** Activity (main) Create, isFirst = true **
java.lang.NoSuchFieldError: android.os.Environment.DIRECTORY_DOCUMENTS
    at de.donmanfred.storage.getPaths(storage.java:45)
    at de.donmanfred.storage.Initialize(storage.java:34)
    at b4a.example.main._activity_create(main.java:327)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at b4a.example.main.afterFirstLayout(main.java:100)
    at b4a.example.main.access$100(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:78)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5457)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
    at dalvik.system.NativeStart.main(Native Method)
Sorry ! I think because of my device. My device is android 4.3.
Thanks!
 
Top