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:

ibra939

Active Member
Licensed User
Longtime User
no simple example ? o_O:rolleyes::confused:
 

DonManfred

Expert
Licensed User
Longtime User

DonManfred

Expert
Licensed User
Longtime User
i think he would like to see a project file...

Then he should make his own project-file! I´m NOT here to do the work for any lazy member...

The code in #1 is enough to do it.
libra939 is member since Jul 7, 2014 and he should be able to do that. If not then i suggest reading the beginners guide or have a look at other examples here in forum. There are hundreds of httputils-examples here, right?
 

ilan

Expert
Licensed User
Longtime User
Then he should make his own project-file! I´m NOT here to do the work for any lazy member...

The code in #1 is enough to do it.
libra939 is member since Jul 7, 2014 and he should be able to do that. If not then i suggest reading the beginners guide or have a look at other examples here in forum. There are hundreds of httputils-examples here, right?

i agree with you ... ;)
 

sergiones

Member
Licensed User
Longtime User
Hi Erel, I know this is an old thread but I'm searching this forum for hours and this post is very near for what I need. I want to download xlsx and pdf from a website that requires cookies. Using cookiemanager I extracted the cookies and sent to job1 using getrequest.setheader.

When JobDone is concluded (succesfully, and I have a server confirmation that the download was made so cookie is okay) I get the problem.

My Url is http://example.com/download?someparameter&id=555

Using the method:
B4X:
Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "filename.dat", False)

How can I know the filename (and extension?)

Thank you!
 

sergiones

Member
Licensed User
Longtime User
The link you provided does not show a filename... You need to get the filename on another way...

Thank you Don, browsers can read the server filename using this same url, maybe some other way to achieve this?

I need to download this files using two cookies. What is the best way to do this?
 

DonManfred

Expert
Licensed User
Longtime User
You can try okhttputils2 sourcecode version and check if the result contains any headers you can get this information from.

You can get the source here https://www.b4x.com/android/forum/threads/okhttp-replaces-the-http-library.54723/#content

In sub ResponseSuccess you then can get the headers....

B4X:
Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
    Response.GetAsynchronously("response", File.OpenOutput(TempFolder, TaskId, False), _
        True, TaskId)
       
       dim m as map = Response.GetHeaders
End Sub
check the Map m if it contains your filename....
 
Last edited:

sergiones

Member
Licensed User
Longtime User
You can try okhttputils2 sourcecode version and check if the result contains any headers you can get this information from.

You can get the source here https://www.b4x.com/android/forum/threads/okhttp-replaces-the-http-library.54723/#content

In sub ResponseSuccess you then can get the headers....

B4X:
Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
    Response.GetAsynchronously("response", File.OpenOutput(TempFolder, TaskId, False), _
        True, TaskId)
      
       dim m as map = Response.GetHeaders
End Sub
check the Map m if it contains your filename....

Thank you Don. I'm not sure if it works since the example requires a filename before the Success event. As asked by Erel I will create a new thread for this problem. Thanks.
 
Top