Android Question Download Images then Save to KVS then Populate xCLV with Wait For

Mahares

Expert
Licensed User
Longtime User
I download 13 small images with their links from a web site.
I use KVS2 to store the bitmaps as values to a file if file is empty (kvs.ListKeys.Size=0 ) in Dir.internal along the links as keys.
I retrieve the images to populate an xCLV from the file..
the only way the xCLV gets populated the first time with kvs2 empty is if I place a Sleep(5000) after the web download finishes and before I populate the xCLV.
Or:
I add this code to the DownloadImage sub without the Wait For pause, which I can live with:
clv.Add(CreateListItem(j.GetBitmapResize(150dip, 150dip,True), clv.AsView.Width, 150dip, Link ), Link)
kvs.PutBitmap(Link, j.GetBitmapResize(150dip, 150dip,True))
I have tried using Wait For at different locations in the code, Can I do away with the Sleep(5000)

Below is the relevant code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
    kvs.Initialize(File.DirInternal,"MyKvs")
    
    If kvs.ListKeys.Size=0 Then
        MyList.Initialize
        MyList=Array("Hannibal.jpg", _
        "Silas021415.jpg", _
        "crossfire061619_3.jpg", "sweden.png", "turkey.png", "mexico.png", "china.png", "canada.png", "bulgaria.png", _
        "denmark.png", "brazil.png", "israel.png", "japan.png" )    
        DownloadImage(MyList)
        Sleep(5000)  'clv does not populate on first try unless I have sleep(5000)
        PopulateCLV
    Else
        PopulateCLV
    End If
End Sub

Sub DownloadImage(Links As List) 
    Dim url As String ="http://xxxxx.com/yyyy/Uploads/"
     For Each Link As String In Links
        Dim j As HttpJob
        j.Initialize("", Me)
        j.Download(url & Link)  'link here is the image file name, we add to it the url
        Wait For (j) JobDone(j As HttpJob)
            If j.Success Then
                kvs.PutBitmap(Link, j.GetBitmapResize(150dip, 150dip,True))
'                Log(j.success)
            Else
                Log("Error: " & j.ErrorMessage)
                ToastMessageShow("Error: " & j.ErrorMessage, True)
            End If
        j.Release
      Next
    Log("downloaded files")
End Sub

Sub PopulateCLV
    For Each Link As String In kvs.ListKeys
        clv.Add(CreateListItem(kvs.GetBitmap(Link), clv.AsView.Width, 150dip, Link ), Link)
    Next
End Sub[code]
 

Mahares

Expert
Licensed User
Longtime User
I spent a lot of time trying to apply the solution in the link you provided. Believe me, I spent a lot of time looking at other threads involving wait for before I attempted to post this thread. That did not help me.
I replaced Sleep(5000). with this:
wait for (DownloadImage(MyList)) Complete (Unused As Int)
but the error returned:
B4X:
Error occurred on line: 83 (Main)
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
line 83 is the line: End Sub in DownloadImage
EDIT: Finally, I used this: wait for (DownloadImage(MyList)) Complete (unused As Object)
It seems to work. Is this the right way. I never thought of using Object before I used Int or Boolean
 
Last edited:
Upvote 0
Top