Android Question Questions on the use of RDC with fields of type BLOB

lhbrito

Member
Licensed User
Longtime User
Through some examples here of the forum I was able to read a table with a blob field (image), insert in an internal sqlite database and display in an activity using the ImageView.

But I thought the code was a bit "confused / complex" and was in doubt if the way I'm doing is correct or is there another simpler / better.

Made as follows (sample):
Table name: COMPLEMENTOS and structure:
IDCOMPLEMENTO, INTEGER
DESCRICAO, TEXT
OBS, BLOB
FOTO, BLOB

1) in the procedure "jobdone":
B4X:
For Each records() As Object in Result.Rows
  Dim dados(4) As Object

  Dados(0) = records(0)
  Dados(1) = records(1)

  Dim Buffer() As Byte
  Buffer = records(2)
  dados(2) = Buffer

  Dim Buffer() As Byte
  Buffer = records(3)
  Dados(3) = Buffer

  SQL1.ExecNonQuery2("INSERT INTO COMPLEMENTOS (IDCOMPLEMENTO,DESCRICAO,OBS,FOTO) VALUES (?,?,?,?)", Dados)
Next
2) View the image in any activity:
B4X:
(...)
     Dim Cursor1 As Cursor
     Cursor1 = SQL1.ExecQuery("SELECT FOTO FROM COMPLEMENTOS WHERE IDCOMPLEMENTO = "&nID)
     If Cursor1.RowCount>0 Then
       Cursor1.Position = 0
       Dim Buffer() As Byte
       Buffer = Cursor1.GetBlob("Foto")
       Dim InputStream1 As InputStream
       InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
       Dim Bitmap1 As Bitmap
       Bitmap1.Initialize2(InputStream1)
       InputStream1.Close
       ImageView1.Bitmap = Bitmap1
     End If
     Cursor1.Close
(...)

The major questions:
a) in the "jobdone": it is necessary to keep the "Dim dados(4) As Object" inside the loop?
b) in the "jobdone": I have problems of out of memory if there are many records?
c) in the "jobdone": read/write blob(text only) is the same when read/write a blob(image)?
d) displaying the image: the only way is to use a sequence:
getBlob => Buffer () => InputStream => BitMap => ImageView ?
e) displaying the image: Can I use dbUtils to simplify?

Thanks!
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
a) Allocating 4 * 4 bytes is a very very small allocation. I wouldn't worry about it at all. You can however move it out of the loop.
b) The problem is with the bitmaps. They are probably too large. You should use InitializeSample instead. You will need to first save the image to a file.

RDC has two utility methods that can simplify this process: BytesToImage and ImageToBytes. However if you need to use LoadBitmapSample then you will need to first save the blob to a file.
 
Upvote 0
Top