Ive got a problem with imagedownloader which is that when I am on 3g, sometimes the images I try to download wont be downloaded, and therefore the imageview wil not show the bitmap downloaded. This problem occurs mainly on slow connections, in which some images are downloaded but other no. Any idea?
Main Activity:
ImageDonwloader class:
Any idea? Thanks
Main Activity:
B4X:
Sub Activity_Create
...
Dim job As HttpJob
job.Initialize("j", Me)
job.Download("http://g3dsoftware.ddns.net:8080/neater/fastfoodnumber.txt")
End Sub
Sub JobDone(job As HttpJob)
If job.Success Then
Dim out As OutputStream = File.OpenOutput(File.DirInternal, "ffnumeroderestaurantes.txt", False)
File.Copy2(job.GetInputStream, out)
out.Close '<------ very important
Dim im(File.ReadString(File.DirInternal,"ffnumeroderestaurantes.txt")+1) As ImageView
Starter.immax=File.ReadString(File.DirInternal,"ffnumeroderestaurantes.txt")
For i = 2 To File.ReadString(File.DirInternal,"ffnumeroderestaurantes.txt")
im(i).Initialize("im")
im(i).Bitmap= bmp
im(i).Tag = "http://g3dsoftware.ddns.net:8080/neater/fastfood"&i&".png"
If i=2 Then
scrollviewff.Panel.AddView(im(i),2%x,30%y,47%x,47%x)
else If i Mod 2 == 0 Then
scrollviewff.Panel.AddView(im(i),2%x,im(i-2).Top+47%x + 2%y,47%x,47%x)
Else
scrollviewff.Panel.AddView(im(i),51%x,im(i-1).Top,47%x,47%X)
End If
im(i).Gravity = Gravity.FILL
Dim links As Map 'is the map neccessary now ?? .. or just pass im(i).Tag value ?
links.Initialize
links.Put(im(i), "http://g3dsoftware.ddns.net:8080/neater/fastfood"&i&".png")
CallSubDelayed2(ImageDownloader, "Download", links)
scrollviewff.Panel.Height= ivff1.Height+filterbanner.Height +(im(i).Height *i/2)
Next
Else
Log("Error: " & job.ErrorMessage)
ToastMessageShow("No se pudo conectar con el servidor. Intenta mas tarde",True)
timer1.Enabled=False
End If
job.Release
End Sub
ImageDonwloader class:
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
cache.Clear
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
Wait For (J) 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
Next
If Job.JobName="http://g3dsoftware.ddns.net:8080/neater/fastfood"&Starter.immax & ".png" Then
CallSub(Main, "imready")
End If
If Job.JobName="http://g3dsoftware.ddns.net:8080/neater/combo"&Starter.immax & ".png" Then
CallSub(Main, "imready2")
End If
End If
Else
If Job.JobName="http://g3dsoftware.ddns.net:8080/neater/fastfood2.png" Then
CallSub(Main,"error")
End If
End If
Job.Release
End Sub