Android Question catch imageDownloader event

dualznz

Member
Licensed User
Longtime User
Hi all is there anyway to catch the event say if the image that was downloaded is say broken it could then be replaced with a default image in the device?

cheers

SERVICE MODULE
Code: Copy
Sub Process_Globals
    Private cache As Map
    Private tasks As Map
    Private ongoingTasks As Map
    Public errorfound As Int
End Sub

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

Sub Service_Start (StartingIntent As Intent)
End Sub

Sub Service_Destroy

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
            Dim iv As ImageView = ImageViewsMap.GetKeyAt(i)
            iv.SetBackgroundImage(cache.Get(link))
        Else If ongoingTasks.ContainsKey(link) = False Then
            ongoingTasks.Put(link, "")
            Dim j As HttpJob
            j.Initialize(link, Me)
            j.Download(link)
        End If
    Next
End Sub

Sub JobDone(Job As HttpJob)
    ongoingTasks.Remove(Job.JobName)
    If Job.Success Then
        Dim bmp As Bitmap = Job.GetBitmap
        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 ImageView = tasks.GetKeyAt(i)
                    iv.SetBackgroundImage(bmp)
                End If
                errorfound = 0
            Next
        End If
    Else
        Log("Error downloading image: " & Job.JobName & CRLF & Job.ErrorMessage)
        errorfound = 1
       
    End If
    Job.Release
End Sub
Sub ActivityIsPaused
    tasks.Clear
End Sub

Main window
Code: Copy
' AIRF logo
    Dim links As Map
    imagetest.Initialize("imagetest")
    links.Initialize
    links.Put(imagetest, "http://syoursite.com/image.jpg")
    CallSubDelayed2(imageDownloader, "Download", links)
    Activity.AddView(imagetest, 0, 0, 40%x, 40%y)
    imagetest.Gravity = Gravity.FILL
   
    imageerror = imageDownloader.errorfound
   
    If imageerror == 1 Then
        Msgbox("error", "")
    Else
        Msgbox("downloaded", "")
    End If
 
Last edited:
like this i add the image under the comment
Code: Copy
Sub JobDone(Job As HttpJob)
    ongoingTasks.Remove(Job.JobName)
    If Job.Success Then
        Dim bmp As Bitmap = Job.GetBitmap
        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 ImageView = tasks.GetKeyAt(i)
                    iv.SetBackgroundImage(bmp)
                    errorfound = 0
                End If
            Next
        End If
    Else
        Log("Error downloading image: " & Job.JobName & CRLF & Job.ErrorMessage)
        ' image goes here
      
    End If
    Job.Release
End Sub
 
Upvote 0
No. Something like:
Code: Copy
Sub JobDone(Job As HttpJob)
    ongoingTasks.Remove(Job.JobName)
  Dim bmp As Bitmap
    If Job.Success Then
      bmp  = Job.GetBitmap
    Else
      bmp = 'default bitmap
    End If
        
        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 ImageView = tasks.GetKeyAt(i)
                    iv.SetBackgroundImage(bmp)
                    errorfound = 0
                End If
            Next
        End If
    Job.Release
End Sub
 
Upvote 0
Back
Top