Android Question A way to know if SD is mounted and its path...

tigrot

Well-Known Member
Licensed User
Longtime User
Hi everybody,
I have written this:
B4X:
Public Sub getSDCardPath() As String
  ' Private rp As RuntimePermissions
   Dim iMount, iCRLF, iSlash, iAuto As Int
   Dim fstab As String = File.ReadString("/system/etc/", "vold.fstab")
   Dim Mount As String
   Dim exten As String=File.DirDefaultExternal
   Dim ptr1 As Int=exten.IndexOf("Android")
   If ptr1=0 Then
        Msgbox ("Unknown directory:" & exten,"Error")
   End If
   Do While True
      'get next line with mount point:
      iMount = fstab.IndexOf2("dev_mount ", iCRLF)
      If iMount < 0 Then Exit
      iCRLF = fstab.IndexOf2(CRLF, iMount)
      If iCRLF < 0 Then Exit
      Mount = fstab.SubString2(iMount, iCRLF)
      'get mount path:
      iSlash = Mount.IndexOf("/")
      If iSlash < 0 Then Continue
      iAuto = Mount.IndexOf2("auto", iSlash)
      If iAuto < iSlash Then Continue
      Mount = Mount.SubString2(iSlash, iAuto).Trim
      'return this one, if new:
      If Mount <> File.DirRootExternal Then
         Try
           Dim files As List=File.ListFiles(Mount)
           If files.IsInitialized Then
              Return Mount & "/" & exten.SubString(ptr1)
           End If
        Catch
        End Try
      End If
  
     Loop
Return File.DirDefaultExternal
End Sub

Thanks jost-aus-soest for the base of this sub. It returns the path to APP's directory on the first SD mounted or internal memory if SD is not present. It doesn't check if APP's directory is created. If there are two external memories mounted its behaviour is not predictable...
I'd like if somebody could check the code under different devices...

Ciao
Mauro
 

tigrot

Well-Known Member
Licensed User
Longtime User
The previous code didn't work for new SD, with no data inside, this one is likely to work...

B4X:
Public Sub getSDCardPath() As String
  ' Private rp As RuntimePermissions
   Dim iMount, iCRLF, iSlash, iAuto As Int
   Dim fstab As String = File.ReadString("/system/etc/", "vold.fstab")
   Dim Mount As String
   Dim exten As String=File.DirDefaultExternal
   Dim ptr1 As Int=exten.IndexOf("Android")
   If ptr1=0 Then
        Msgbox ("Unknown directory:" & exten,"Error")
   End If
   Do While True
     
      'get next line with mount point:
      iMount = fstab.IndexOf2("dev_mount ", iCRLF)
      If iMount < 0 Then Exit
      iCRLF = fstab.IndexOf2(CRLF, iMount)
      If iCRLF < 0 Then Exit
      Mount = fstab.SubString2(iMount, iCRLF)
     
      'get mount path:
      iSlash = Mount.IndexOf("/")
      If iSlash < 0 Then Continue
      iAuto = Mount.IndexOf2("auto", iSlash)
      If iAuto < iSlash Then Continue
      Mount = Mount.SubString2(iSlash, iAuto).Trim
     
      'return this one, if new:
      Log(Mount)
      Log(File.DirDefaultExternal)
      If Mount <> File.DirRootExternal Then
          Try
        File.WriteString( Mount & "/" & exten.SubString(ptr1),"txt.txt","test")
        File.Delete( Mount & "/" & exten.SubString(ptr1),"txt.txt")
        Return Mount & "/" & exten.SubString(ptr1)
        Catch
            Log("Do nothing")
        End Try
      End If
     Loop
   Return File.DirDefaultExternal
End Sub
 
Upvote 0
Top