Downloading many files with HttpJob

AlpVir

Well-Known Member
Licensed User
Longtime User
I created a procedure (with HttpJob) that will download from the internet of a large (100-200) series of JPG images whose name is stored in a list; the content of this list is not fixed. After download, the files are then saved in the smartphone.
This procedure is very complex because I had to take into account that not all images of the list can be actually present on the internet.
However, it has a serious flaw: every time you run this procedure it will download ALL of the images, even if they are already present in the smartphone.
How to avoid the substantial burden of work?
Something like:

B4X:
Sub DownloadFile ()
  For i = 0 To FileList.Size-1
Arc = FileList.Get (i)
  If File.Exists (File.DirRootExternal, Arc) = False then
  end if
  next
end sub


Many thanks in advance
 

AlpVir

Well-Known Member
Licensed User
Longtime User
The following code is correct and produces the desired result: to download a series of images from the web and save them locally.
But it is very complicated (even though I wrote it!) And I can not put the statement
if File.exists .....
as the operation of HttpJop is asynchronous (or at least I think it is).
I am aware that outside help is very difficult but we try anyway.
In a nutshell, the problem is: how to avoid re-download existing images?

B4X:
Sub BotDownload_click
  NomeFile="Dummyi.jpg"
     FileDummy=NomeFile
     job1.Initialize("job1", Me)
    job1.Download(Sito & NomeFile)
End Sub

Sub JobDone(Job As HttpJob)
  Dim C  As String
   Dim rs2  As Cursor
   Dim T  As Long
   Dim sGG, GG, MM, AA As Int, Dt As String 
  
   If Job.Success = True Then
    '--- visualizzo il file
     If NomeFilePrec<>"" AND NomeFilePrec<>FileDummy Then
       If File.Exists (CartellaLocale , NomeFilePrec) Then
        IV.Bitmap = LoadBitmap (CartellaLocale,NomeFilePrec)
         IV.Gravity = Gravity.FILL
         Log ("Visualizzo " & NomeFilePrec)
         TotImg=TotImg+1
         LbTotale.text=CStr(TotImg)&"/"&CStr(NumTotGiteImmagini)
       End If
     End If
    NomeFilePrec=NomeFile
    
     C="SELECT IDEff,Nome,Data FROM TabGite WHERE  IDEff=" & CStr(N)
     rs2 = Main.dbTaccuino.ExecQuery(C)
     If rs2.RowCount > 0 Then
       rs2.Position = 0
       LbGita.Text = rs2.GetString ("Nome")
       DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss"
       T = DateTime.DateParse(rs2.GetString("Data")) :       sGG=DateTime.GetDayOfWeek(T)-1  :    GG=DateTime.GetDayOfMonth(T)  :       MM=DateTime.GetMonth(T) :       AA=DateTime.GetYear(T)  :      Dt= GG & "/" & MM & "/" & AA
       LbData.Text = Dt
     End If
     rs2.Close
  Dim Out As OutputStream
  Out = File.OpenOutput(CartellaLocale , NomeFile, False)
  Job.GetBitmap.WriteToStream(Out, 100, "JPEG")
  i = i + 1
     NomeFile = CStr(N) & "Pers" & Chr(64+i) & ".jpg" :  NomeFile=NomeFile.Replace ("A","")
  If i < 9 Then 
         Log("A  " & NomeFile & "  " & CStr(Posizione))
  job1.Download(Sito & NomeFile)
     Else
       If N=Ultimo Then
         Msgbox ("Il download delle immagini è terminato","FINE DOWNLOAD")
         If File.Exists (CartellaLocale,FileDummy) = True Then
           File.Delete (CartellaLocale,FileDummy)
         End If
         Activity.Finish
      End If
      
       Posizione=Posizione+1
       rs.Position=Posizione
       N=rs.GetLong("IDEff")
       i=1
      NomeFile = CStr(N) & "Pers" & Chr(64+i) & ".jpg" :  NomeFile=NomeFile.Replace ("A","")
       Log("B " & NomeFile & "  " & CStr(Posizione))
      job1.Download(Sito & NomeFile)
  End If
   Else
    '--- qui si arriva (credo) se di tenta di caricare un'immagine che non c'è, ad esempio
     '  la sesta immagini quando ne esistono solo 4
     '  Quindi si passa al record successivo (Posizione=Posizione+1)
     Posizione=Posizione+1
     rs.Position=Posizione
     N=rs.GetLong("IDEff")
     i=1
    NomeFile = CStr(N) & "Pers" & Chr(64+i) & ".jpg" :  NomeFile=NomeFile.Replace ("A","")
     Log("C " & NomeFile & "  " & CStr(Posizione))
    job1.Download(Sito & NomeFile)
  End If
End Sub

The file names follow a rule. Here is a small list their :

1307Pers.jpg
1307PersB.jpg
1307PersC.jpg
1307PersD.jpg
1307PersE.jpg
1307PersF.jpg
1307PersG.jpg
1307PersH.jpg
1309Pers.jpg
1309PersB.jpg
1309PersC.jpg
1309PersD.jpg
1310Pers.jpg
1311Pers.jpg
1311PersB.jpg
1311PersC.jpg
1311PersD.jpg
1311PersE.jpg
1311PersF.jpg
1311PersG.jpg
1311PersH.jpg
1312Pers.jpg
1313Pers.jpg
1313PersB.jpg
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
Indeed ImageDownloader can upload unlimited images.
Maybe I have not understood the great potential of B4A.
I managed to do it all. changing ImageDownloader.
Everything works as desired.
Thank you very much, Erel.

B4X:
    For N=1302 To 1410
   For i=1 To 8
            S=CStr(N) & "Pers" & Chr(i+64) & ".jpg"
            NomeImmagine=S.Replace ("A","")
            If File.Exists (CartellaLocale,NomeImmagine) = False Then
                Dim iv2 As ImageView
                iv2.Initialize ("")
                links.Put(iv2, Sito & NomeImmagine)
            End If
        Next
    Next

B4X:
If link = Job.JobName Then
    Dim iv As ImageView = tasks.GetKeyAt(i)
    iv.SetBackgroundImage(bmp)
         S=Job.JobName
         D=sf.InString (S,"ImmaginiPers/")
         F=sf.Mid (S,D+14,S.Length - D -13 )
         Log ("Download : " & F)              
         Dim out As OutputStream
         out = File.OpenOutput(File.DirRootExternal & "/AlpVirData/ImmaginiPers",F,False )
         File.Copy2(Job.GetInputStream, out)
         out.Close
End If
 
Upvote 0
Top