Android Question Ho to read available space on GetSafeDirDefaultExternal ?

amorosik

Expert
Licensed User
To have the directory where to write the logs that the app produces, I use the following instructions:

Dim rp As RuntimePermissions
log_dir = rp.GetSafeDirDefaultExternal ("App1")

How to read how much space is still available on the drive where the log_dir resides?
 

Mahares

Expert
Licensed User
Longtime User
More "general".
Mario, perhaps easier on the eye like this:
B4X:
Log(GetFolderFreeSpace(xui.DefaultFolder))  'displays: 20,999 MB for mine
B4X:
Public Sub GetFolderFreeSpace(Folder As String) As String
    Dim jo As JavaObject
    jo.InitializeNewInstance("java.io.File", Array(Folder))
    Return $"${NumberFormat(jo.RunMethod("getUsableSpace", Null)/1024/1024,1,0)} MB"$
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Mario, perhaps easier on the eye like this:
I did that (used NumberFormat2) but outside of the function, in the test project; I think it is more "correct" (you can use the function to do calculations)
B4X:
Public Sub GetFolderTotaleSpace(Folder As String) As Long
    Dim jo As JavaObject
    jo.InitializeNewInstance("java.io.File", Array(Folder))
    Return jo.RunMethod("getTotalSpace", Null)
End Sub
B4X:
Public Sub GetFolderFreeSpace(Folder As String) As Long
    Dim jo As JavaObject
    jo.InitializeNewInstance("java.io.File", Array(Folder))
    Return jo.RunMethod("getUsableSpace", Null)
End Sub
B4X:
Public Sub GetFolderFreeSpacePercent(Folder As String) As Long
    ' REQUIRES:
    '    GetFolderTotaleSpace
    '    GetFolderFreeSpace
    Dim TotalSpace As Long = GetFolderTotaleSpace(Folder)
    Return (100 - ((TotalSpace - GetFolderFreeSpace(Folder) ) / TotalSpace) * 100)
End Sub
 
Last edited:
Upvote 0
Top