Android Code Snippet [B4X] DownloadAndSave

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User
This is a slightly modified sub based on the code from: [B4X] OkHttpUtils2 with Wait For

Call it when you want to download a file and save it. You can wait for it to complete if needed.
B4X:
Sub DownloadAndSave (Url As String, Dir As String, FileName As String) As ResumableSub
   Dim j As HttpJob
   j.Initialize("", Me)
   j.Download(Url)
   Wait For (j) JobDone(j As HttpJob)
   If j.Success Then
       Dim out As OutputStream = File.OpenOutput(Dir, FileName, False)
       File.Copy2(j.GetInputStream, out)
       out.Close
   End If
   j.Release
   Return j.Success
End Sub
Usage:
B4X:
Wait For (DownloadAndSave(URL, File.DirInternal, "map_large.jpg")) Complete (Success As Boolean)
If Success Then
 ...
 

aidymp

Active Member
Licensed User
Can the progress (of the download) be exposed? to use in a progress bar?

Thanks
 
Last edited:

Erel

Administrator
Staff member
Licensed User
Yes. Not with this exact code and it shouldn't be discussed here.
 
Status
Not open for further replies.
Top