Android Question Download multiple files from web server and save on device

u2005k

Member
Licensed User
Longtime User
Hi,

In our App, it is required to download multiple files from web server and save them on Android device.
I looked at HTTPUtils2 and other examples but I am not finding good way of getting files (it can Word, Excel, PDF or image files). These set of files are unique to each App user and number will not be constant.

Each individual file url is already available in App local SQLite database table. I need to process these records and get files and save them in local App folder.

I don't need exact code but just approach to this problem.

Thanks & regards...
U2005K
 

DonManfred

Expert
Licensed User
Longtime User
I don't need exact code but just approach to this problem.
ok. Here is the spproach

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim todownload As List
End Sub
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    todownload.Initialize
    ' Query the database for all downloadlinks and put them into the list
    ' todownload
        ' HERE you need to fill the list with the downloadurls

Start the first download after you fill the list with

B4X:
    If todownload.Size > 0 Then
        Dim furl As String = todownload.Get(0)
        Log("Download: "&furl)
        Dim j As HttpJob
        j.Initialize("DownloadFile",Me)
        j.Tag = furl
        j.Download(furl)
    End If

B4X:
Sub JobDone(Job As HttpJob)
    ProgressDialogHide
    If Job.Success Then
        Dim res As String
        res = Job.GetString
        Log("JobName: "&Job.JobName)
        If Job.JobName = "DownloadFile" Then
            Dim m As String = Job.Tag
      Dim OutStream As OutputStream
      Log("DownloadReady: "&Job.Tag)
      OutStream = File.OpenOutput(File.DirRootExternal, GetFilename(m), False)
      File.Copy2(Job.GetInputStream,OutStream) ' save the file
      OutStream.Close
      Log(GetFilename(m)&" is written to "&File.DirRootExternal)
            ' Remove the first job from queue
            If todownload.Size > 0 Then
                todownload.RemoveAt(0)
            End If

            ' Check if there are more files to download. If yes. Download them
            If todownload.Size > 0 Then
                Dim furl As String = todownload.Get(0)
                Log("Download: "&furl)
                Dim j As HttpJob
                j.Initialize("DownloadFile",Me)
                j.Tag = furl
                j.Download(furl)
            Else
                LogColor("======================================",Colors.Green)
                LogColor("===== ALL FILES FROM QUERE     =======",Colors.Green)
                LogColor("===== ARE FINISHED DOWNLOADING =======",Colors.Green)
                LogColor("======================================",Colors.Green)
            End If

                       
        End If
           
       
    Else
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
Sub GetFilename(fullpath As String) As String
   Return fullpath.SubString(fullpath.LastIndexOf("/") + 1)
End Sub

Hope this helps
 
Upvote 0

Similar Threads

Top