Android Code Snippet [B4X] DownloadAndSave

Status
Not open for further replies.
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
 ...
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…