B4J Question How to write data in ImageView to a BLOB record in SQLite DB

Diego Marcelo Croatto

Member
Licensed User
Hi... How is the procedure to store a image in a ImageView into a BLOB register?

ScreenCapture.png


I don't understand how type of data put in the SQL chain (Productos=Table) .... Codigo (Filter Record) ... Imagen (BLOB Record)


SQLite store:
        SQLString="UPDATE Productos SET Imagen='"& ????????? &"' where Codigo=('BUB12500004')"
        Log(SQLString)
        SQL1.ExecNonQuery(SQLString)
If any can aid me... thank's a Lot!!!
 

drgottjr

Well-Known Member
Licensed User
B4X:
    Dim bmp As Bitmap = LoadBitmap(File.DirAssets, "YOUR_IMAGE_FILE")
    ' CONVERT TO BYTES
    Dim b() As Byte = ImageToBytes(bmp)
    
   ' CREATE TABLE
    sql.Initialize( File.DirInternal, "test.db", True )
    sql.ExecNonQuery("create table if not exists test(col1 VARCHAR(256), col2 BLOB)")

    ' STORE RECORD (WITH IMAGE AS BLOB)
    sql.ExecNonQuery2("insert into test values(?, ?)", Array As Object("SOME_NAME", b))
    
    ' NOW GO SELECT THE RECORD AND SHOW AS IMAGE IN IMAGEVIEW
    rs = sql.ExecQuery("select * from test")
    Dim fname As String
    Dim bytes() As Byte
    Do While rs.NextRow
        fname = rs.GetString( "col1")
        bytes = rs.GetBlob( "col2" )
    Loop
    
    Log( "got: " & fname)
    Dim imageview As ImageView
    imageview.Initialize("")
    Activity.AddView(imageview, 0%x,0%y, 100%x,100%y)
    'CONVERT BLOB BYTES BACK TO BITMAP AND ASSIGN TO IMAGEVIEW
    imageview.Bitmap = BytesToImage( bytes )

' -------------------------------------------------------------------------------------------------------------
Public Sub ImageToBytes(Image As Bitmap) As Byte()
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    Image.WriteToStream(out, 100, "JPEG")
    out.Close
    Return out.ToBytesArray
End Sub

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