Android Code Snippet [B4X] DownloadAndSave

Discussion in 'Code Snippets' started by Erel, Feb 12, 2019.

Thread Status:
Not open for further replies.
  1. Erel

    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.
    Code:
    Sub DownloadAndSave (Url As String, Dir As String, FileName As StringAs 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:
    Code:
    Wait For (DownloadAndSave(URL, File.DirInternal, "map_large.jpg")) Complete (Success As Boolean)
    If Success Then
     ...
     
  2. aidymp

    aidymp Active Member Licensed User

    Can the progress (of the download) be exposed? to use in a progress bar?

    Thanks
     
    Last edited: Oct 29, 2019
    jahswani likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    Yes. Not with this exact code and it shouldn't be discussed here.
     
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice