Find Total, Used and Free Memory on SdCard

rtesluk

Member
Licensed User
Longtime User
Sep 27 2011

Hi

With Adroid Level 8 emulator I could use this code

p.Shell("df", Array As String("sdcard"), sb, Null)

to obtain the total, used and free memory on SdCard.

But with Level 9 or higher it (the code) crashes.

I did a adb -e shell df (in DOS) on both a Level 8 and a Level 9 emulator.

I have attached the results of these shell commands for your pursual and you can see that the outputs (the format) are quite different.

Is there any way that I can parse the output of Level 9 higher and get the same information as I can with Level 8?

The information with Level 9 and higher has the current status of the memory on the SdCard but in a different format.

Ray Tesluk :sign0163:
 

Attachments

  • Adb_df_Level_8_Level_9.jpg
    Adb_df_Level_8_Level_9.jpg
    31.8 KB · Views: 409

rtesluk

Member
Licensed User
Longtime User
A Solution

Sep 28 2011
04:50 Hours

Erel, I took your advice and created this solution (The botton code where p.SdkVersion > 8. Thank you. :sign0188:
B4X:
Sub CheckSDCard(type_ As String)
Dim p As Phone
Dim sb As StringBuilder
Dim splitLine() As String
Dim splitLine1() As String
Dim splitLine2() 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

   If p.SdkVersion <= 8 Then 
      Try 
         '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
      Catch
         Msgbox(LastException,"FreeSdCard p.Shell SdkVer <= 8")
      End Try
      Return splitline1(0).Trim
   Else
      Try 
         'split memory info lines
         splitLine = Regex.Split("/t",sb.ToString)
         splitLine1 = Regex.Split("sdcard",splitLine(0))
         splitLine2 = Regex.Split("[ ]+",splitLine1(1).Trim)
         Select Case type_
            Case "total"
               Return splitLine2(0).Trim
            Case "used"
               Return splitLine2(1).Trim
            Case "available"
               Return splitLine2(2).Trim
            Case Else
               Return splitLine2(3).Trim
         End Select
      Catch
         Msgbox(LastException,"FreeSdCard p.Shell SdkVer > 8")
      End Try
   End If
End Sub
 
Upvote 0

rtesluk

Member
Licensed User
Longtime User
What are
B4X:
 [\code] where do you put them[/b]

[quote="Erel, post: 66122"]Thank you for sharing your solution. I've added [ code ] [ /code ] tags (without spaces) to make it easier to read.[/QUOTE]

I would like to know how you do this tagging for future reference - it certainly is easier to read for the viewers - Ray :):)
 
Upvote 0

rtesluk

Member
Licensed User
Longtime User
I would like to know how you do this tagging for future reference - it certainly is easier to read for the viewers - Ray :):)

Oh, I see what you did to my response.

B4X:
my lines of code etc.

[\Code]

Thanks again Erel:sign0188:
 
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
Upvote 0

rtesluk

Member
Licensed User
Longtime User
Upvote 0
Top