Android Question [SOLVED] jRDC & sqlite image blob retrieval and display in imageview

matt humphreys

Member
Licensed User
Longtime User
SOLVED:
B4A project using jRDC on local and remote servers just fine however I can't nut out how to successfully convert a retrieved blob from sqLite db and display image in an imageview.

the update/insert side is this:

B4X:
Dim req as dbRequestManager
Dim sSQL() As String
Dim Buffer() As Byte

'--load the test image
ivJobImg.Bitmap=LoadBitmapResize(Starter.shared,"image1.jpg",ivJobImg.Width, ivJobImg.Height,True)
 
If ivJobImg.IsInitialized Then
    Buffer = req.ImageToBytes(ivJobImg.Bitmap)
End If

sSQL=Array As String(Array As Object(Buffer))

returns job.success so the saving of image seems to work just fine.
The sqlite viewer displays the data as Ljava.lang.Object;@2e0fe16

The retrieval side is this:
B4X:
Dim req as dbRequestManager
Dim Blob as object
Dim Buffer() As Byte
Dim ser As B4XSerializator
Blob = row(res.Columns.Get("Img"))
Buffer=ser.ConvertObjectToBytes(Blob)
ivJobImg.SetBackgroundImage(req.BytesToImage(Buffer)) '<--errors on this call

..and the sub from dbRequestManager
B4X:
'Converts a bytes array to an image (for BLOB fields).
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) '<-----Line 117---in dbRequestManager
    Return bmp
End Sub

The error I get is this:
Error occurred on line: 117 (DBRequestManager)
java.lang.RuntimeException: Error loading bitmap.
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:539)
at b4a.RDCclient.dbrequestmanager._bytestoimage(dbrequestmanager.java:297)

The sqlite field type as BLOB does not seem to make any difference.
Im my mind I'm supposed to be feeding the "BytesToImage" sub with an array of bytes.
Appreciate some fresh eyes if possible...Thanks
 
Last edited:

matt humphreys

Member
Licensed User
Longtime User
Update:
using erels SQLiteViewer I get the same error when trying to view the image so it appears to be in how it is being stored, not the retrieval.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
B4X:
Dim req as dbRequestManager
Dim Blob() as Byte
Blob = row(res.Columns.Get("Img"))
ivJobImg.SetBackgroundImage(req.BytesToImage(Blob))

Assumption: Your image field in the database is of type blob. If so, JRDC2's RDCHanlder will use SQL's ResultSet GetBlob2 method (https://www.b4x.com/b4j/help/jsql.html#resultset_getblob2) that returns an array of bytes.
 
Upvote 0

matt humphreys

Member
Licensed User
Longtime User
SOLVED!
several things wrong.

For the insert/update I passed parameters incorrectly to begin with so the data in SQLite was not actually a blob.

As above, I was using this:
B4X:
sSQL=Array As String(str1,str2,int1,int2,Array As Object(Buffer))

When I should be using this:
B4X:
dim objParams as Object
Dim Buffer() as Byte
'--Test that ImageView has an image so we don't pass Null to 'dbRequestManager.ImageToBytes' sub
'--LoadLayout initialises imageView so cannot use .isInitialised
If ivJobImg.Bitmap<>Null  Then
       Buffer = req.ImageToBytes(ivJobImg.Bitmap)
End If
objParams=Array As Object(str1,str2,int1,int2,Buffer)

Thanks OliverA for input and making me look closer at jRDCHandler ...I learnt something.
The Affinity type on BLOB field containing a NULL is 0 so it never invokes GetBlob2 method until it actually gets a Blob in there.
In this case then the
B4X:
Dim Blob() as Byte
Blob= row(res.Columns.Get("Img"))
returns a Null which, as noted in my first post, was upsetting the 'dbRequestManager.BytesToImage' sub

so I needed to add that test to returned data as such:
B4X:
Blob= row(res.Columns.Get("Img"))
If Blob<>Null Then
      If Blob.Length>0 Then
             Blob.SetBackgroundImage(req.BytesToImage(Blob))
      End If
End If


If that helps anyone....
 
Upvote 0
Top