Android Code Snippet Download image or file from a website

Discussion in 'Code Snippets' started by Erel, Mar 30, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Edit: It is recommended to use this code instead: [B4X] OkHttpUtils2 with Wait For

    Description: Download an online image and show it as the activity background.

    Code:
    Dim job As HttpJob
    job.Initialize(
    "j", Me)
    job.Download(<link>)

    Sub JobDone(job As HttpJob)
       
    If job.Success Then
         
    'we can check the of value job.JobName if there is more than one job
         Activity.SetBackgroundImage(job.GetBitmap)
       
    Else
         
    Log("Error: " & job.ErrorMessage)
       
    End If
       job.Release
    End Sub
    Depends on HttpUtils2 library

    Description: Download an online file and copy it to the external storage.
    Code:
    Dim job As HttpJob
    job.Initialize(
    "j", Me)
    job.Download(<link>)

    Sub JobDone(job As HttpJob)
       
    If job.Success Then
         
    Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "filename.dat"False)
         
    File.Copy2(job.GetInputStream, out)
         out.Close 
    '<------ very important
       Else
         
    Log("Error: " & job.ErrorMessage)
       
    End If
       job.Release
    End Sub
    Tags: download, http, httputil2, web
     
    Last edited: Jun 15, 2017
  2. DonManfred

    DonManfred Expert Licensed User

    @Erel use TAGS in this forum please :)
     
    LucaMs, Peter Simpson and Erel like this.
  3. Beja

    Beja Expert Licensed User

    Hi Erel,

    What's "filename.dat"?

    Is it a place holder for the url or it is a new name for the downloaded file? if so then how about if the downloaded file ends with a different extension?
    Thanks
     
  4. DonManfred

    DonManfred Expert Licensed User

    It´s just a placeholder to fill with the name you want the file save to...
     
    Rajesh Gupta likes this.
  5. Beja

    Beja Expert Licensed User

    Thanx Don,
     
  6. Beja

    Beja Expert Licensed User

    Well thought, Erel.. thanks a million.
     
  7. ilan

    ilan Expert Licensed User

    its not working for me the file that is downloaded is 0 KB big
    and i get error when i try to load the bitmap to imageview

    this is the code i am using

    Code:
    Dim job11 As HttpJob
    job11.Initialize(
    "Job11", Me)
    job11.Download(
    "http://www.sagital.net/shabbat/myadd.png")

    Code:
    Select Job.Jobname
    ......
    Case "Job11"

                
    Try
                    
    Dim out3 As OutputStream = File.OpenOutput(File.DirDefaultExternal, "myadd.png"False)
                    
    File.Copy2(Job.GetInputStream, out3)
                    out3.Close
                
    Catch
                
    Log("job11 sucess")
                
    End Try  
              
            
    End Select

    i tried also this: myaddimg.SetBackgroundImage(job.GetBitmap)

    but also no success, what am i doing wrong?

    this is the error i get in the log:

    Code:
    java.lang.NullPointerException
        at www.sagitalcashlite.net.menu._imagesjobdone(
    menu.java:24081)
        at www.sagitalcashlite.net.menu._jobdone(
    menu.java:24607)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    511)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    174)
        at anywheresoftware.b4a.keywords.Common$
    5.run(Common.java:952)
        at android.os.Handler.handleCallback(Handler.java:
    725)
        at android.os.Handler.dispatchMessage(Handler.java:
    92)
        at android.os.Looper.loop(Looper.java:
    137)
        at android.app.ActivityThread.main(ActivityThread.java:
    5283)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    869)
        at dalvik.system.NativeStart.main(Native Method)
     
    Last edited: Jul 15, 2014
  8. Erel

    Erel Administrator Staff Member Licensed User

    Are you checking the value of Job.Success?
     
  9. ilan

    ilan Expert Licensed User

    yes erel i do

    Code:
    If Job.Success = True Then
            
    Select Job.JobName
                      ...
                
    Case "Job11"
                
    Try

                    myaddimg.Bitmap  = Job.GetBitmap
                    myaddimg.Gravity = 
    Gravity.FILL
                
    Catch
                
    Log("job11 sucess")
                
    End Try          
            
    End Select
    this is not working...
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Run your app in Debug mode to see the exact error line.
     
  11. ilan

    ilan Expert Licensed User

    this is the line: myaddimg.Bitmap = Job.GetBitmap

    this is the error:

    Code:
    menu_jobdone (B4A line: 6526)


    End Sub
    java.lang.NullPointerException
        at www.sagitalcashlite.net.menu._jobdone(
    menu.java:24594)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    511)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    174)
        at anywheresoftware.b4a.keywords.Common$
    5.run(Common.java:952)
        at android.os.Handler.handleCallback(Handler.java:
    725)
        at android.os.Handler.dispatchMessage(Handler.java:
    92)
        at android.os.Looper.loop(Looper.java:
    137)
        at android.app.ActivityThread.main(ActivityThread.java:
    5283)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    869)
        at dalvik.system.NativeStart.main(Native Method)
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Have you initialized myaddimg?
     
  13. ilan

    ilan Expert Licensed User

    No, because this is a view i have added in the designer (imageview) ihave some images in dirassets and i can set them as the background image of the imageview but via job.getimage its not working
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    I don't think that the error is in Job.GetImage. However this is not the place to discuss this issue. Please start a new thread in the questions forum and post the relevant code.
     
  15. ilan

    ilan Expert Licensed User

    thanx erel i found the problem

    i was using HttpUtils2 v2.00 and HttpJob v2.00
    and i updated them to HttpUtils2 v2.01 and HttpJob v2.01 and its working now
     
  16. zeuspower

    zeuspower Member Licensed User

    Hello !
    I am trying to download a small db file (~300k) to dir.defaultexternal
    the link is something like this ( fake):

    http://jo-phones.net/ggps_phones_sqlite.db

    The file is downloaded OK ! and it works OK inside my app !

    But I am not getting : jobdone log message ...and also i see that there is a constant communication in my wifi-android-icon

    What i am doing wrong ?

    Code:
    Sub cloud_DB_update_clicky

    Dim job As HttpJob
    job.Initialize(
    "j", Me)

    job.Download(
    "http://jo-phones.net/ggps_phones_sqlite.db")
    job.

    End Sub
    Sub JobDone (job As HttpJob)
      
    If job.Success Then
        
    Dim out As OutputStream = File.OpenOutput(File.DirDefaultExternal, "ggps_phones_sqlite.db"False)
        
    File.Copy2(job.GetInputStream, out)
        out.Close 
    '<------ very important
        Log("FILE DOWNLOADED ! ")
      
    Else
        
    Log("Error: " & job.ErrorMessage)
      
    End If
      job.Release
    End Sub
     
  17. ilan

    ilan Expert Licensed User

    your are not getting it because u are not asking for it

    put this above if job.success then...

    Code:
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
     
  18. zeuspower

    zeuspower Member Licensed User

    Nop,nothing ! I even tested the original demo of httputils2 , no logs also !

    But if i put a toastmessage it works OK...something is wrong with my bridge wi-fi,maybe ?
     
  19. ilan

    ilan Expert Licensed User

    are u using a real device or via simulator?
     
  20. zeuspower

    zeuspower Member Licensed User

    real..with Bridge of version Gunter Gabriel
     
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