Android Question Insert Image into SQL DB from URL in XML

melvin2345

Member
Licensed User
Longtime User
Let's say I'm reading an XML file using httputils. The containers are <name> and <logo>.

As I'm parsing the XML, I want to download the image referenced in <logo>, and then insert both <name> and the downloaded image into a table defined as (Name TEXT, Logo BLOB).

I tried an httputils call for the logo in the parser_endelement sub, but that just messed everything up.

I assume this sort of thing is possible, I'm just clearly going about it all wrong. Can anyone point me in the right direction?
 

KMatle

Expert
Licensed User
Longtime User
I assume the tag <logo> contains the image's filepath & name like www.myserver/logos/logo1234.jpg

Get the full path and follow this example: https://www.b4x.com/android/forum/threads/download-image-or-file-from-a-website.39412/

You can copy the file's content with

B4X:
Dim FileAsByteArray() As Byte
Dim su As StringUtils
Dim fc64s As String
FileAsByteArray=Job.GetInputStream
fc64s=su.EncodeBase64(FileAsByteArray)

to an byte array and then to Base64. After that you have a single string which can be stored in your table.

When you select it later from the table, just decode it back to a byte array and e.g. load it into a bitmap to show it (or write it to a file, etc.)
 
Upvote 0

melvin2345

Member
Licensed User
Longtime User
I assume the tag <logo> contains the image's filepath & name like www.myserver/logos/logo1234.jpg

Get the full path and follow this example: https://www.b4x.com/android/forum/threads/download-image-or-file-from-a-website.39412/

You can copy the file's content with

B4X:
Dim FileAsByteArray() As Byte
Dim su As StringUtils
Dim fc64s As String
FileAsByteArray=Job.GetInputStream
fc64s=su.EncodeBase64(FileAsByteArray)

to an byte array and then to Base64. After that you have a single string which can be stored in your table.

When you select it later from the table, just decode it back to a byte array and e.g. load it into a bitmap to show it (or write it to a file, etc.)

Yes, it has the full URL. I copied the code from the example you linked to, and copied your code as well, but I keep getting:

B4X:
javac 1.7.0_03
src\b4a\example\main.java:617: error: inconvertible types
_fileasbytearray = (byte[])(_imgjob._getinputstream().getObject());
                           ^
  required: byte[]
  found:    InputStream
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
have you tried something like
B4X:
        Dim i As InputStream = Job.GetInputStream
        Dim arr(i.BytesAvailable) As Byte
        i.ReadBytes(arr,0,i.BytesAvailable)
        ' arr will hold the bytes
 
Upvote 0

melvin2345

Member
Licensed User
Longtime User
I ended up using a bit of code that Erel posted in another thread.

B4X:
    If ImgJob.Success Then
        Dim LogoOutput As OutputStream
        LogoOutput.InitializeToBytesArray(1000)
        File.Copy2(ImgJob.GetInputStream, LogoOutput)
        Dim Buffer() As Byte
        Buffer = LogoOutput.ToBytesArray
        'Msgbox(ImgJob.JobName, "Vendor ID")
        TCDData.ExecNonQuery2("update TCDeals set Logo = ? where VendorID = '"&ImgJob.JobName&"'", Array As Object(Buffer))
   End If

I set the job name as the ID on the database row I was working on, and simply did an update on the row that was initially created during the XML parse. Not sure if this is the most efficient means, but it works.
 
Upvote 0
Top