Android Code Snippet Download image or file from a website

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.

B4X:
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.
B4X:
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:

DonManfred

Expert
Licensed User
Longtime User
@Erel use TAGS in this forum please :)
 

Beja

Expert
Licensed User
Longtime 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
 

DonManfred

Expert
Licensed User
Longtime 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?

It´s just a placeholder to fill with the name you want the file save to...
 

Beja

Expert
Licensed User
Longtime User
Well thought, Erel.. thanks a million.
 

ilan

Expert
Licensed User
Longtime 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

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


B4X:
  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:

B4X:
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:

ilan

Expert
Licensed User
Longtime User
yes erel i do

B4X:
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...
 

ilan

Expert
Licensed User
Longtime User
this is the line: myaddimg.Bitmap = Job.GetBitmap

this is the error:

B4X:
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)
 

ilan

Expert
Licensed User
Longtime 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
 

ilan

Expert
Licensed User
Longtime 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
 

zeuspower

Member
Licensed User
Longtime 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 ?

B4X:
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
 

ilan

Expert
Licensed User
Longtime User
your are not getting it because u are not asking for it

put this above if job.success then...

B4X:
Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
 

zeuspower

Member
Licensed User
Longtime 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 ?
 
Top