iOS Question (Solved) SQLite BLOB to imageView?

anOparator

Active Member
Licensed User
Longtime User
What's the recommended way to get a BLOB (image) from SQLite.
I'm having zero success modifying this one approach that I found.
Am trying to port a working B4A sub to B4I.
B4X:
Sub BlobToImageView
   resultSet4 = SQL1.ExecQuery("SELECT image FROM table1 WHERE rowID = " & resultSet4.GetInt("ID"))
   Dim binimageR() As Byte, OtptR As InputStream
'   Cursor1.Position = 0                               '  Cursor and Position not used in B4I
   resultSet4.GetInt("id")                               '  column with the id index
   Log("2prntr NMBR pls = " & (myRowID))
   binimageR = resultSet4.GetBlob("image")               '  column with the image BLOB
   OtptR.InitializeFromBytesArray(binimageR, 0, binimageR.Length)
   BmpR.Initialize2(OtptR)
   OtptR.Close
   Dim outR As OutputStream = File.OpenOutput(File.DirLibrary, "4.png" , False)  ' I know this is wrong
   BmpR.WriteToStream(outR, 100, "JPEG") ' write the bitmap to the stream
   outR.Close
   resultSet4.Close
End Sub
Thanks in advance
 

anOparator

Active Member
Licensed User
Longtime User
Just trying to get BLOBs from SQLite onto an ImageView. Of course it can be done in B4I.

' I have forgotten, if I ever knew, how to call this sub, or what this bytes() array would be, but I did hack at it for hours:
B4X:
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

No compile errors for the attached zip file, probably cause it's not doing anything.
B4X:
 imvNxt_Click:
'   imvNxt.Bitmap = bmp           ' Error: Object was Not initialized (UIImage),  imvNxt made in Designer
   imvNxt.Bitmap = rs           ' Error: Expected: UIImage, object type: B4IResultSet

Anyone seen any examples or articles on doing this?
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
Now getting 'Error loading file', for me it seems like progress.
B4X:
Sub notGettingBLOB
    Log(" prntr errror B4 here, Yes/No")
'   Dim CursR As ResultSet = SQL1.ExecQuery("SELECT image FROM imgz WHERE RowID = " & lstRowIDs.Get(pos))
   Dim CursR As ResultSet = SQL1.ExecQuery("SELECT image FROM imgz")                                           '  Error loading image
   Dim binimageR() As Byte, OtptR As InputStream                                                               '  beyond bounds

'   CursR.Position = 0
   CurrentIndex = RowID
   binimageR = CursR.GetBlob("image")
   OtptR.InitializeFromBytesArray(binimageR, 0, binimageR.Length)
   BmpR.Initialize2(OtptR)       '  Error loading image                             ' stops here <
'   OtptR.Close                   '  Error loading image
   Dim outR As OutputStream = File.OpenOutput(File.DirDocuments, "Picturer.jpg" , False)
   BmpR.WriteToStream(outR, 100, "JPEG") ' write the bitmap to the stream
   outR.Close
   CursR.Close           '  Error loading image
   btnN.Bitmap = LoadBitmap(File.DirLibrary,"Picturer.jpg")
End Sub
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
I think I found it , 2.1.19 Read an image in B4xSQLiteDatabaseV1_4.pdf.
Tomorrow I compile, for now I sleep.
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
This is a solution I came up with, tested on simulators.
Comments and suggestions are always welcome.
As always, thanks for the link, it had a lot of keywords new to me.
B4X:
 Sub ReadBlob
   'Using ExecQuery2 is safer as it escapes special characters automatically.
   'In this case it doesn't really matter.
   rs = sql1.ExecQuery("SELECT thepic FROM table1 WHERE rowID = " & (RowID + 1))  ' **
   rs.NextRow
   Public Buffer() As Byte 'declare an empty byte array
   Buffer = rs.GetBlob("thepic")
   Public InputStream1 As InputStream
   InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
   Public Bitmap1 As Bitmap
   Bitmap1.Initialize2(InputStream1)
   InputStream1.Close
   ImageView1.Bitmap = Bitmap1
End Sub
 

Attachments

  • BlobToImv.zip
    295 KB · Views: 261
Upvote 0
Top