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

Discussion in 'Android Questions' started by matt humphreys, Apr 14, 2018.

  1. matt humphreys

    matt humphreys Member Licensed 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:

    Code:
    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:
    Code:
    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
    Code:
    '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:
    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: Apr 15, 2018
  2. matt humphreys

    matt humphreys Member Licensed 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.
     
  3. OliverA

    OliverA Expert Licensed User

    Code:
    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.
     
  4. matt humphreys

    matt humphreys Member Licensed 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:
    Code:
    sSQL=Array As String(str1,str2,int1,int2,Array As Object(Buffer))
    When I should be using this:
    Code:
    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
    Code:
    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:
    Code:
    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....
     
  5. KMatle

    KMatle Expert Licensed User

    Mostly it is easier to convert the bytes to a Base64 string. More storage is needed, but it's easy to handle.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice