Android Question Return from Jobdone

Marco Maria Vilucchi

Active Member
Licensed User
Longtime User
Hi all,
I have a problem.
I read data from a DB MySql using HttpJob.
Then I have a loop to read several items from DB
For each item I need to get a picture from an URL (the name of the picture is in the DB)
To get item I need to use HttpJob Download.
Then my result is on Jobdone again.
But how can I return to the prior loop to read next item?
There is another way to get a picture from URL?
Here the code:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim jobx As HttpJob
    Main.Qry="Select * FROM sp01negozi where sp01key>'0' order by sp01key"
    jobx.Initialize("Job0", Me)
    jobx.PostString(Main.ServerUrl, Main.Qry)

End Sub

Private Sub RiempiItem
    For i = 0 To Main.rows.size - 1
        m = Main.rows.Get(i)
        sp01immagine = m.Get("sp01immagine")

        job3.Initialize("Job3", Me)
        job3.Download("http://www.b4x.com/forum/images/categories/android.png")
        .......
        .......
        .......
    next
End Sub

Sub JobDone (Job As HttpJob)
    If Job.Success = True Then
        Select Job.JobName
            Case "Job0"

                Dim parser As JSONParser
                Dim response As String
                response = Job.GetString
                parser.Initialize(response)
                Main.rows = parser.NextArray
                 RiempiItem
        
                 Case "Job3"
                    'show the downloaded image
                    ??????
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

Thanks
Marco
 

Emme Developer

Well-Known Member
Licensed User
Longtime User
I suggest you to use Wait for (b4a 7+) to download the image.. i don't understand your question, job.download is async, so meanwhile you are downloading the image the loop is in execution.. If you want to download one image and after the next image, you can consider to put in job.tag the list, and instead of call for i = 0 to list.size you can call a sub if index < list.size and every time you get JobDone you can call the same sub with current index + 1
 
Upvote 0

Marco Maria Vilucchi

Active Member
Licensed User
Longtime User
I'm using this code now.
I haven't errore, but picture is not displaied

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim Foto As ImageView
    DownloadImage(Main.ServerDirImmagini & sp01immagine, Foto)
    scvPersons.Panel.AddView(Foto, 5dip,5dip,50dip,50dip)
End Sub

Sub DownloadImage(Link As String, iv As ImageView)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        iv.Bitmap = j.GetBitmap
    End If
    j.Release
End Sub
 
Upvote 0

Emme Developer

Well-Known Member
Licensed User
Longtime User
Seems the code is right.. have you checked the image from browser?
You can try
B4X:
iv.Bitmap = j.GetBitmapResize(iv.width,iv.height,true)
iv.gravity = true

Can you see the imageview?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You need to understand the code. It is not so complicated. The DownloadImage sub downloads an image and sets it to an ImageView. If you need to do something else then do something else with the image:
B4X:
 Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim bmp As Bitmap = j.GetBitmap 'best option is to use GetBitmapResize. If it is not available because you are using an older version of B4A then use GetBitmapSample
        'Do whatever you like with the image
    End If
 
Upvote 0

Emme Developer

Well-Known Member
Licensed User
Longtime User
Yes, the image is here:
http://www.mamavisoftware.it/spinacetoshopping/immagini/evento1.jpg
I used code linked by Erel above.
How can I use your code? I Have an error on GetBitmapResize.
Then if I put your code into DownloadImage I haven't iv, if I put it into Activity_Create I haven't j.
Thanks in advance
Marco
You are getting an error in GetBitmapResize because you're calling DownloadImage before adding the imageView into a parent

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim Foto As ImageView
    scvPersons.Panel.AddView(Foto, 5dip,5dip,50dip,50dip)
DownloadImage(Main.ServerDirImmagini & sp01immagine, Foto)

End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Please note that the program flow is as follows:

Call DownloadImage sub
In DownloadImage sub, process until wait for
Return to where DownloadImage sub was called from
Add Foto to view (notice that the image has not been downloaded yet!)
After image is downloaded, finish DownloadImage sub

So you are adding Foto to the view before it even contains the downloaded image.

One way to accomplish what you are trying to do is as follows:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim Foto As ImageView
    Wait For (DownloadImage(Main.ServerDirImmagini & sp01immagine, Foto)) Complete (Result as Boolean)
    If Result = True Then
        scvPersons.Panel.AddView(Foto, 5dip,5dip,50dip,50dip)
    End If
End Sub

Sub DownloadImage(Link As String, iv As ImageView) As ResumableSub
    Dim success As Boolean = False
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        iv.Bitmap = j.GetBitmap
        success = True
    End If
    j.Release
    Return success
End Sub

Note: I just modified your code, so this may contain typing/logic errors.

This code will wait for DownloadImage to complete before adding Foto to the view.
 
Upvote 0
Top