Android Question B4A to B4X Pages question

yiankos1

Well-Known Member
Licensed User
Longtime User
Hello team,

I try to port an old project to B4X Pages and i have a simple question. I used below service to download multiple images from my server:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    Private cache As Map
    Private tasks As Map
    Private ongoingTasks As Map
End Sub

Sub Service_Create
    tasks.Initialize
    cache.Initialize
    ongoingTasks.Initialize
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy
    Log("ImageDownloader Service Destroyed")
End Sub

Sub Download (ImageViewsMap As Map)
    For i = 0 To ImageViewsMap.Size - 1
        tasks.Put(ImageViewsMap.GetKeyAt(i), ImageViewsMap.GetValueAt(i))
        Dim link As String = ImageViewsMap.GetValueAt(i)
        If cache.ContainsKey(link) Then
            Log("Cached image")
            Dim iv As BetterImageView = ImageViewsMap.GetKeyAt(i)
            iv.ScaleType=iv.SCALETYPE_CENTER_CROP
            iv.Bitmap=cache.Get(link)
        Else If ongoingTasks.ContainsKey(link) = False Then
            ongoingTasks.Put(link, "")
            Dim j As HttpJob
            j.Initialize(link, Me)
            'j.Download(link)
            j.Download2(Starter.downloadLink,Array As String("name", link & ".jpg"))
        End If
    Next
End Sub

Sub JobDone(Job As HttpJob)
   
    ongoingTasks.Remove(Job.JobName)
    If Job.Success Then
        Dim bmp As B4XBitmap = Job.GetBitmapResize(310dip,270dip,True)
        cache.Put(Job.JobName, bmp)
        If tasks.IsInitialized Then
            For i = 0 To tasks.Size - 1
                Dim link As String = tasks.GetValueAt(i)
                If link = Job.JobName Then
                    Dim iv As BetterImageView = tasks.GetKeyAt(i)
                    iv.Bitmap=bmp
                    iv.ScaleType=iv.SCALETYPE_CENTER_CROP
                End If
            Next
        End If
    Else
        'Log("Error downloading image: " & Job.JobName & CRLF & Job.ErrorMessage)
        Dim bmp As B4XBitmap = LoadBitmapResize(File.DirAssets,"image-broken.png",40dip,40dip,True)
        If tasks.IsInitialized Then
            For i = 0 To tasks.Size - 1
                Dim link As String = tasks.GetValueAt(i)
                If link = Job.JobName Then
                    Dim iv As BetterImageView = tasks.GetKeyAt(i)
                    iv.Bitmap=bmp
                    'iv.ScaleType=iv.SCALETYPE_CENTER
                End If
            Next
        End If
    End If
    Job.Release

End Sub

Sub ActivityIsPaused
    tasks.Clear
End Sub
Now my question is, where should i add this code in order to download couple of images? In each B4X page class?

Moreover, using B4X Pages will not freeze app while image downloading right? (Thats why i used services)

Thank you for your time.
 

LucaMs

Expert
Licensed User
Longtime User
Now my question is, where should i add this code in order to download couple of images? In each B4X page class?
Why? In one B4XPage, which is, as you wrote, a class (I mean, you don't need to show it, to use Root & Views).

Moreover, using B4X Pages will not freeze app while image downloading right?
Correct.
 
Upvote 0

yiankos1

Well-Known Member
Licensed User
Longtime User
Why? In one B4XPage, which is, as you wrote, a class (I mean, you don't need to show it, to use Root & Views).


Correct.
Thank you very much for your information!
 
Upvote 0
Top