B4J Question Store image to SQLite (from HTMLeditor and Clipboard)

ThRuST

Well-Known Member
Licensed User
Longtime User
I want to store an image into a SQLite database using B4J and also load it back, let's say the image is in a HTMLeditor1 control. Store an image directly from the clipboard is also an option I would like to cover. Any advice on how it's done?

I zipped up my small source code example for you to play around with post your changes as a zip thanks. You will also get my B4JUtils library as a code module. It makes it very easy to create/access the application folder and subfolders.

btw the loaded byte array should show the image in ImageView2.

Solution in v1.1 Thanks to MarkusR for providing help for the solution. Erel contributed with help as well.

v1.2 contains the new improved code provided by Erel and MarkusR. Check it out :)
 

Attachments

  • BLOB.zip
    92.6 KB · Views: 339
  • BLOB v1.1.zip
    94.7 KB · Views: 332
  • BLOB v1.2.zip
    94.8 KB · Views: 380
Last edited:

ThRuST

Well-Known Member
Licensed User
Longtime User
I am trying to modify Sub ReadBlob for B4A in post#7 to work with B4J but I cannot get it to show the bytearray as an image in Imageview1.

My SQLite database is simple for this test. Table1 holds Id=1 and Image holds the byte array.

How can I successfully load it back to show the image in ImageView1 ?

I tried this

B4X:
Sub ReadBlob
 
    Dim RS1 As ResultSet = SQL.ExecQuery("SELECT * FROM Table1 where Id = 1")

    Buffer1 = RS1.GetBlob("Image")
    RS1.Close
 
    ImageView1.SetImage (Buffer1)

End Sub

Please add in the corrections to make this work. Thanks

blob.JPG
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
how do you defined the Buffer1 ?
it should be a byte array.
B4X:
Dim Buffer() As Byte

untested
B4X:
Public Sub BytesToImage(bytes() As Byte) As Image
   Dim In As InputStream
   In.InitializeFromBytesArray(bytes, 0, bytes.Length)
   Dim bmp As Image
   bmp.Initialize2(In)
   Return bmp
End Sub
 
Last edited:
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I use this

B4X:
Sub InsertBlob
    'convert the image file to a bytes array
    'Dim InputStream1 As InputStream
    'InputStream1.Clipboard.GetImage
    Dim InputStream1 As InputStream
    'InputStream1 = fx.Clipboard.GetImage
    InputStream1 = File.OpenInput(File.DirAssets, "car.jpg")
    Dim OutputStream1 As OutputStream
    OutputStream1.InitializeToBytesArray(1000)
    File.Copy2(InputStream1, OutputStream1)
    
    Buffer = OutputStream1.ToBytesArray
    
    Log(Buffer.Length)
    'ImageView1.SetImage(Buffer)
End Sub
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
B4X:
Public Buffer1 As ImageView

I assume this is not correct. Please help me :)
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
try
B4X:
Dim Buffer1() As Byte
then you have the file but you need converted it into a Image.
i edited my post above.
i edited my post above again hhh
 
Last edited:
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I upload my source code for the example in post #1, please download it and add the changes then post it here. I'd like to see your changes to make it work.

Honor and reputation awaits, brave coding knight :)
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
ah,ok,later but i made a example
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
        
    Dim st As InputStream  = File.OpenInput(File.DirAssets,"1.png")
    
    Dim bytes(st.BytesAvailable) As Byte
    st.ReadBytes(bytes,0,st.BytesAvailable)
    
    Dim img As Image
    img = BytesToImage(bytes)
    
    ImageView1.SetImage(img)
    
End Sub

Public Sub BytesToImage(bytes() As Byte) As Image
    Dim In As InputStream
    In.InitializeFromBytesArray(bytes, 0, bytes.Length)
    Dim bmp As Image
    bmp.Initialize2(In)
    Return bmp   
End Sub
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Nice. I will try that. But please add it to my source example to make it work then post here :)
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Dear Erel please try my source in post #1, it concerns loading a stored byte array from a SQLite database :)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is mostly trivial to write and read blobs.

This code:
B4X:
Dim InputStream1 As InputStream
InputStream1 = File.OpenInput(File.DirAssets, "car.jpg")
Dim OutputStream1 As OutputStream
OutputStream1.InitializeToBytesArray(1000)
File.Copy2(InputStream1, OutputStream1)

Buffer = OutputStream1.ToBytesArray
Can be replaced with:
B4X:
Buffer = Bit.InputStreamToBytes(File.OpenInput(File.DirAssets, "car.jpg"))
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I see. I found this code somewhere in the forum and the post was probably old.

How to load the byte array from SQLite Table1 Id=1 and from Image

blob.JPG


I started a sub that looks like this (which is in the source code example)

B4X:
Sub ReadBlob
  
    Dim RS1 As ResultSet = SQL.ExecQuery("SELECT * FROM Table1 where Id = 1")
    Buffer1 = RS1.GetBlob("Image")
    RS1.Close
  
End Sub

Should I use GetBlob or GetString since it's a byte array?
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Please download and try BLOB v1.1 in post#1. It's a working solution. It converts an image to byte array and stores the data in a SQLite database, and retrieves the byte array and displays the image in ImageView2. If you want to contribute with your own solution that you think is even better then please post your modifications here. Thanks

I did not find any solution for this for B4J so this should be the first working solution for B4J. Yay :)
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
here :) for your blob project example from first page
left button save the blob and right button load it
B4X:
' *******************************************
' * Road buffer from database
' *******************************************
Sub ReadBlob
   
    Dim RS1 As ResultSet = SQL.ExecQuery("SELECT * FROM Table1 where Id = 1")
    Dim Buffer1() As Byte 'declares an empty array
    Buffer1 = RS1.GetBlob("Image")
   
    Dim St As InputStream
    St.InitializeFromBytesArray(Buffer1,0,Buffer1.Length)
    Dim Image As Image
    Image.Initialize2(St)
    St.Close
   
    RS1.Close
   
    ImageView2.SetImage(Image)
   
End Sub

' *******************************************
' * Convert buffer to display image
' *******************************************
Sub Button2_Click

    ReadBlob
    'fx.Msgbox(MainForm, "Put correct code here!!", "")  
       
End Sub
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Btw
B4X:
RS1.GetBlob("Image")
must be used and not GetString as I was asking about earlier. This example is awesome to learn from.
I hope we get many likes from everybody who reads this thread :)
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I'd like to cover the question also concerning turning an image into byte array from the clipboard memory. The solution we covered was from having an image stored on the harddrive but there's scenarios when the image data from clipboard should be handled. Any advice on this?

Based on Erel's solution this might work

B4X:
Buffer = Bit.InputStreamToBytes(fx.Clipboard.GetImage)

But that generates this error

BLOB_error.JPG
 
Last edited:
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
see
fx.Clipboard.GetImage.WriteToStream(
the stream is a .png "file" ... bytes
 
Upvote 0
Top