Share My Creation codemodule:CheckSDCard

Here is an example to get SD Card space.

Place CheckSDCard in code module.

Please upload improvements

useage:

Log(codemodule.CheckSDCard("total"))
Log(codemodule.CheckSDCard("used"))
Log(codemodule.CheckSDCard("available"))

If codemodule.CheckSDCard("available") > 10000 Then
Log("Enough memory to install")
End If

' codemodule

B4X:
Sub CheckSDCard(type_ As String)
Dim p As Phone
Dim sb As StringBuilder
Dim splitLine() As String
Dim splitLine1() As String
Dim s1 As String

   ' Initialize stringbuilder
     sb.Initialize
  
     ' get memory info of sd card
     p.Shell("df", Array As String("sdcard"), sb, Null)  ' free space
   
   'split memory info lines
   splitLine=Regex.Split(",",sb.ToString)
    If type_="total" Then
      splitLine1=Regex.Split("K",splitLine(0))
      s1=splitline1(0)
      s1=s1.Replace("sdcard: ","")
      splitline1(0)=s1
       'Log(splitline1(0))
   Else If type_="used" Then
      splitLine1=Regex.Split("K",splitLine(1))
       'Log(splitline1(0))
   Else If type_="available" Then
      splitLine1=Regex.Split("K",splitLine(2))
       'Log(splitline1(0))
   End If
   
   Return splitline1(0).Trim

End Sub
 

francoisg

Active Member
Licensed User
Longtime User
Need to know if the device has sdcard inserted / temp space available ???

Hi,
this is my take on the issue (needed to know if there is space available on the sdcard / sdcard is inserted), else the app needs to save temp files on device memory ... EDIT: Does not seem to work on all devices, try 2'nd function to see if sdcard is inserted (IF GetDeviceAvailable ("sdcard") Then ...) ...

B4X:
' *** Returns amount of avalable space on specified device (in kb) ...
Sub GetAvailableSpace (DeviceName As String) As Long
    Dim ph as Phone
    Dim sb As StringBuilder
    sb.Initialize
    ' *** Returns: device_name: xxxK total, yyyyK used, zzzK available (block size nnnn)
    ph.Shell("df | grep " & DeviceName, Null, sb, Null)  ' *** Return free space of specified device only ...
    If (sb.ToString <> "") Then Return Regex.Split("K available", Regex.Split(",", sb.ToString)(2))(0) else return -1
End Sub

' *** Returns data directory: internal if no sdcard present, else data directory of sdcard ...
Sub GetDataDir As String
    If (GetAvailableSpace ("sdcard") > 0) Then Return File.DirDefaultExternal Else Return File.DirInternal
End Sub
B4X:
' *** Returns true if the specified device is mounted, false if not ...
Sub GetDeviceAvailable (DeviceName As String) As Boolean
    Dim sb As StringBuilder
    sb.Initialize
    ph.Shell("mount", Null, sb, Null)
    Return (sb.ToString.IndexOf(DeviceName) > -1)
End Sub
 
Last edited:
Top