B4J Question Disk Space Utils magic keys

peacemaker

Expert
Licensed User
Longtime User
HI, All

I'm trying to use the code module https://www.b4x.com/android/forum/threads/disk-space-utils.47520/#content
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    '(ArrayList) [C:\, D:\, E:\, P:\, R:\]
    '(MyMap) {C:\=126100873216, D:\=125829115904, E:\=104857595904, P:\=258932207616, R:\=104857595904}
    '(MyMap) {C:\=30034104320, D:\=34107752448, E:\=20868218880, P:\=16199786496, R:\=20868218880}
    '(MyMap) {C:\=30034104320, D:\=34107752448, E:\=20868218880, P:\=16199786496, R:\=20868218880}
    '(MyMap) {C:\=96066768896, D:\=91721363456, E:\=83989377024, P:\=242732421120, R:\=83989377024}
   
    Dim drive As String = File.DirData("").SubString2(0, 3)
    Log("drive=" & drive)
   
    Private TotalDrives As List
    TotalDrives.Initialize
    TotalDrives=DiskUtils.GetDrives
    Log (TotalDrives)
   
    Private TotalDriveCapacity As Map
    TotalDriveCapacity.Initialize
    TotalDriveCapacity=DiskUtils.GetDrivesCapacity
    Log (TotalDriveCapacity)
   
    Private TotalUsableSpace As Map
    TotalUsableSpace.Initialize
    TotalUsableSpace=DiskUtils.GetUsableSpace
    Log (TotalUsableSpace)
   
    Private TotalFreeSpace As Map
    TotalFreeSpace.Initialize
    TotalFreeSpace=DiskUtils.GetFreeSpace
    Log(TotalFreeSpace)
   
    Private Writable As Map
    Writable.Initialize
    Writable=DiskUtils.Writable
   
    Private UsedSpace As Map
    UsedSpace.Initialize
    UsedSpace=DiskUtils.GetUsedSpace
    Log(UsedSpace)
   
    Log(TotalUsableSpace.ContainsKey(drive))    'Always FALSE ?!
End Sub

But cannot identify the drive - the letter "C:\" cannot be found in the map keys:

Result::
Waiting for debugger to connect...
Program started.
drive=C:\
(ArrayList) [C:\, D:\, E:\, P:\, R:\]
(MyMap) {C:\=126100873216, D:\=125829115904, E:\=104857595904, P:\=258932207616, R:\=104857595904}
(MyMap) {C:\=30033149952, D:\=34108186624, E:\=20867784704, P:\=16199708672, R:\=20867784704}
(MyMap) {C:\=30033149952, D:\=34108186624, E:\=20867784704, P:\=16199708672, R:\=20867784704}
(MyMap) {C:\=96067723264, D:\=91720929280, E:\=83989811200, P:\=242732498944, R:\=83989811200}
false

Some magic ...
Any suggestion ?
 

amykonio

Active Member
Licensed User
Longtime User
Try changing:
B4X:
Log(TotalUsableSpace.ContainsKey(drive))    'Always FALSE ?!
to:
B4X:
Log(TotalUsableSpace.ContainsKey(drive.As(Object)))    'Always FALSE ?!
It should work.

Andreas.
 
Upvote 0

amykonio

Active Member
Licensed User
Longtime User
The problem must be related to your key in GetUsableSpace -> mUsableSpace is an object.
I guess ContainsKey dosn't cast the value?

Andreas.
 
Upvote 0

amykonio

Active Member
Licensed User
Longtime User
If you modify GetUsableSpace to:
B4X:
' returns a Map with the following format (mapKey=driveLeter,value=Total Usable Space of the drive in BYTES)
Sub GetUsableSpace As Map
    Private mUsableSpace As Map
    mUsableSpace.Initialize
    Private JObj1,JObj2 As JavaObject
    JObj1.InitializeStatic("java.io.File")
    JObj1 = JObj1.RunMethod("listRoots",Null)
    Private Drives() As Object
    Drives = JObj1
    For i = 0 To Drives.Length-1
        Dim a As String
        a=Drives(i)
        JObj2.InitializeNewInstance("java.io.File",Array As Object (a))
        JObj2=JObj2.RunMethod("getUsableSpace",Null)
        mUsableSpace.Put(a,JObj2)  '<--- HERE IS MY CHANGE
    Next
    Return mUsableSpace
End Sub
it should work.

I mean with the following call:
B4X:
Log(TotalUsableSpace.ContainsKey(drive))
But I don't know if you wanna change the module.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
I replied above with the link, thanks.
 
Upvote 0
Top