B4J Question Download images from website

TomDuncan

Active Member
Licensed User
Longtime User
Hi All,
Can someone guide me on how to download images from websites.
I need to grab a sites images. I can get these from sources.
Tom
 

MarkusR

Well-Known Member
Licensed User
Longtime User
Upvote 0

TomDuncan

Active Member
Licensed User
Longtime User
Checked out the download demo. How do I save the image. This is a non UI program.
Also I need to add some text to the image as a time stamp.
 
Last edited:
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
u need to create the file name with correct extention or using a sql database or make a info text file to the image.

see DateTime and DateUtils

create a simple file from string variable
B4X:
File.WriteString(File.DirApp, "1.txt", "Some text")

save an image
B4X:
    Dim Job As HttpJob
    '...

    'Dim Image1 As Image

    Dim Out As OutputStream = File.OpenOutput(File.DirApp, "1.png")
    
    Job.GetBitmap.WriteToStream(Out) 'from web request

    'Image1.WriteToStream(Out) 'from image direct
    
    Out.Close
 
Upvote 0

TomDuncan

Active Member
Licensed User
Longtime User
This is my modified code and it's error.

B4X:
Sub DownloadImage(Link As String, filename As String)
    Dim job As HttpJob
    job.Initialize("", Me) 'note that the name parameter is no longer needed.
    job.Download(Link)
    Wait For (job) JobDone(job As HttpJob)
    If job.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirApp, "test.jpg", False)
           job.GetBitmap.WriteToStream(out) 'from web request
        out.Close
    End If
    job.Release
End Sub

B4X:
B4J Version: 6.30
Parsing code.    (0.04s)
Compiling code.    (0.13s)
Compiling layouts code.    (0.00s)
Organizing libraries.    (0.00s)
Compiling generated Java code.    Error
B4J line: 134
job.GetBitmap.WriteToStream(out) 'from web re
javac 1.8.0_181
src\b4j\example\main.java:423: error: cannot find symbol
_job._getbitmap().WriteToStream((java.io.OutputStream)(_out.getObject()));
    ^
  symbol:   method _getbitmap()
  location: variable _job of type httpjob
1 error

Also the text is to super some text over the image.

Tom
 
Upvote 0

TomDuncan

Active Member
Licensed User
Longtime User
I think the problem is that this is an non-ui application.
If I use jOkHttpUtils2_NONUI then job.GetBitmap.WriteToStream(out) comes with an error
unknown member: getbitmap
This is a NonUI web server program

Tom
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Try
B4X:
Sub DownloadImage(Link As String, filename As String)
    Dim job As HttpJob
    job.Initialize("", Me) 'note that the name parameter is no longer needed.
    job.Download(Link)
    Wait For (job) JobDone(job As HttpJob)
    If job.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirApp, "test.jpg", False)
        File.Copy2(job.GetInputStream, out)
        out.Close
    End If
    job.Release
End Sub

Note: This is only going to properly work if all your downloading is an image (in this case a JPG image), not an HTML file containing an image and/or image links.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
Make sure to use the latest version of jOkHttpUtils2.
jOkHttpUtils2 - v2.40
jOkHttpUtils2_NONUI - v2.40 < agree this GetBitmap Is missing here
ide said v2.62, have b4j 6.30 installed
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
agree this GetBitmap Is missing here
Image (in B4J) is a UI component (jFX), therefore GetBitmap is not part of the non-ui version of the library (it would just throw an error).
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
Image (in B4J) is a UI component (jFX), therefore GetBitmap is not part of the non-ui version of the library (it would just throw an error).
ah, that is the reason :)
(for me is a bitmap just pixel data in bytes)
 
Upvote 0
Top