Android Code Snippet Download image or file from a website

ibra939

Active Member
Licensed User
Longtime User
no simple example ?
 

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

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

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