Android Question JdbcSQL with BLOB field

Wendell Carneiro Mendes

Member
Licensed User
Longtime User
Goodnight

I made a small app for my own use, connecting to a remote MS SQL SERVER database through a fixed IP.
I use JdbcSQL and everything works perfectly fine, but I can't open an image field (BLOB) to an imageview.
I found several examples, but they all use local database.
I use the following code:

B4X:
Dim sf As Object = msSQLDADOS.ExecQueryAsync("msSQLDADOS", "SELECT Photo FROM Produtos Where  idProduto = '000004'" , Null)
Wait For (sf) msSQLDADOS_QueryComplete (Success As Boolean, Crsr As JdbcResultSet)
If Success = True Then
    Do While Crsr.NextRow
        Dim Buffer() As Byte = Crsr.GetBlob("Photo")
        If Buffer <> Null And Buffer.Length > 1 Then
            Dim IpSt As InputStream
            Dim Bitmap1 As Bitmap
            IpSt.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
            Bitmap1.Initialize2(IpSt)
            IpSt.Close
            Activity.SetBackgroundImage(Bitmap1)
        End If
    Loop
End If

The error returned is as follows (in the code above it would be line 10) :

Error occurred on line: 1353 (Main)
java.lang.RuntimeException: Error loading bitmap.
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:539)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA$2.run(BA.java:387)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8751)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)

can anybody help me?

Thank you in advance
 

PaulMeuris

Active Member
Licensed User
This code works for me in some cases where rs is the resultset from the query...
B4X:
        Dim Buffer() As Byte  = rs.GetBlob(colid)
        Log(Buffer.Length)
        Dim InputStream1 As InputStream
        InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
        Dim bmp As Bitmap
        bmp.Initialize2(InputStream1)
        InputStream1.Close
        ivphotodialog.Bitmap = bmp
The ivphotodialog is the imageview.
 
Upvote 0

Wendell Carneiro Mendes

Member
Licensed User
Longtime User
This code works for me in some cases where rs is the resultset from the query...
B4X:
        Dim Buffer() As Byte  = rs.GetBlob(colid)
        Log(Buffer.Length)
        Dim InputStream1 As InputStream
        InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
        Dim bmp As Bitmap
        bmp.Initialize2(InputStream1)
        InputStream1.Close
        ivphotodialog.Bitmap = bmp
The ivphotodialog is the imageview.
Thanks for the answer
I use it this way and I have the same error on the line "bmp.Initialize2(InputStream1)"
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I use it this way and I have the same error on the line "bmp.Initialize2(InputStream1)"
are you sure the data in the blob is correct?

Do a test and create a hexdump for the bytes and post the hexdump please.

B4X:
    Dim bc As ByteConverter
    Dim hex As String = bc.HexFromBytes(Buffer)
 
Upvote 0

PaulMeuris

Active Member
Licensed User
In my database manager tutorial i used the following code to change a blob field in the database.
After this code was executed i was able to use the code from message #2 to read the blob.
B4X:
        ivphotodialog.Bitmap = LoadBitmapResize(exportfolder,"angelfish.jpg",300dip,300dip,True)
        Dim bmp As Bitmap = ivphotodialog.Bitmap
        Dim OutputStream1 As OutputStream
        OutputStream1.InitializeToBytesArray(1000)
        bmp.WriteToStream(OutputStream1, 90, "JPEG")
        Dim Buffer() As Byte = OutputStream1.ToBytesArray
        db.change_field_content(tablename,Array As Object(Buffer),rowid,colid)
You could try to change the blob field in the database to see if this works.
You should also examine the blob bytes to see if it is an image (as @DonManfred suggests three minutes ago)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
After this code was executed i was able to use the code from message #2 to read the blob.
he already answered that - even with your code - the same error appears.

That was leading me to suggest the HEX-Dump-Test to see if it is an Image (or getting any ideas what could be wong).
 
Upvote 0

PaulMeuris

Active Member
Licensed User
In my database manager tutorial i did some testing with images from a test database dbdemos.db3.
The blobs in this database were 'old' windows bmp images. I didn't find a solution how to read the blob at the time.
But thanks to @DonManfred suggestion to read the hex dump i found a solution.
Hex string from buffer:
        Dim bc As ByteConverter
        Dim hex As String = bc.HexFromBytes(Buffer)
        Log(hex)
        Dim InputStream1 As InputStream
        If hex.Contains("FFD8FF") Then        ' jpg, jpeg
            InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
        End If
        If hex.Contains("424D") Then        ' bmp
            InputStream1.InitializeFromBytesArray(Buffer, 8, Buffer.Length)
        End If
        If hex.Contains("47494638") Then    ' gif
            InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
        End If
        If hex.Contains("4D4D002A") Or hex.Contains("49492A00") Then    ' tiff
            InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
        End If
        If hex.Contains("89504E470D0A1A0A") Then    ' png
            InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
        End If
When the buffer contains "424D" then the image should be a bmp-file. The "424D" value was found after offset 8.
So when the program reads from offset 8 the image is retrieved and shown.
Try it with your blobs. It might work.
 
Upvote 0

Wendell Carneiro Mendes

Member
Licensed User
Longtime User
are you sure the data in the blob is correct?

Do a test and create a hexdump for the bytes and post the hexdump please.

B4X:
    Dim bc As ByteConverter
    Dim hex As String = bc.HexFromBytes(Buffer)
Hi and thanks for the help...
Logging as suggested resulted in this:

6C740000F6A80000424DF6A8000000000000360000002800000078000000780000000100180000000000C0A8000000000000000000000000000000000000C89106CA9205CE9203D29302D49400D49400D69300D69300D49203D39403D49602D09000CF8F01C78A0ABF871CB27F235D3E09290F00463D30797671777274837E7B716C6D5455534F4D4C4947464D4B4A52504F4E4C4B4E4C4B52504F514F4E504E4D504E4D504E4D504E4D504E4D504E4D4F4D4C4F4D4C4F51454F50464F50464F50464F50474F50474F4F494F4F494E51484D50474B4E454A4D444A4C464B4D474C4D494D4E4A4F4D4D4F4D4D4D4D4D4D4D4D4D4D4D4D4D4D4D4D4D4D4D4D4C4D4B4D4E4C4E4F4D4F504E4F4F4F4E4E4E4C4C4C4B4B4B4C4D4B4C4D4B4C4D4B4D4E4C4D4E4C4E4F4D4E4F4D4E4F4D504E4D504E4D504E4D504F4B504F4B504F4B504F4B50504A52524C4C4C4649494351504C4C4B474A48475D5B5A615F5F7974757D7D7D7B7678837C79807A75352F22200102623617B27B36BD8819D0950AD99506D89300D99506D08804D28B05D79401D89700D99900D89700D29303CD9004CC9106CC9407C89106CA9205CE9203D19201D49400D49400D69300D49400D49203D39404D49504D09000CF8F01C78A0CBF871CB07F235A3E08290E00453C32797671787375837E7B716C6D5354524F4D4C4947464D4B4A52504F4E4C4B4E4C4B52504F504E4D504E4D504E4D504E4D504E4D504E4D4F4D4C4F4D4C4F4D4C4F50464F50464F50464F50474F50474F50474F4F494F4F494E51484D50474B4D474A4C464A4C464B4C484C4D494D4E4A4F4D4C4D4E4C4D4E4C4D4E4C4D4E4C4D4E4C4D4E4C4D4E4C4C4D4B4D4E4C4E4F4D4F504E4F504E4E4E4E4C4C4C4B4B4B4E4C4B4E4C4B4F4D4C4F4D4C4F4D4C504E4D504E4D504E4D504E4D504E4D504E4D504F4B504F4B504F4B504F4B50504A51514B4C4C464A4A4451504C4C4B474A48475C5A596260607A75767E7E7E797678847D7A817A77352F22200102603717B27B36BD8819D0940CD79507D89300D99506CE8904D08C05D79401D89700D99900D69700D29303CE9004CC9105CC9407CA9106CC9105CE9203D19201D49400D49400D49400D49302D29303D39404D49504CE8F00CD8F03C5890DBD861DAE7F245A3D0A280E00463D33787872787375837E7B706B6C525351504E4D4A48474D4B4A52504F4E4C4B4E4C4B514F4E504E4D504E4D504E4D504E4D504E4D4F4D4C4F4D4C4F4D4C4F4D4C4F50474F50474F50474F50474F50474F4F494F4F494F4F494E504A4D4F494B4D474A4B474A4B474B4C484C4D494D4E4C4D4E4C4D4E4C4D4E4C4D4E4C4D4E4C4D4E4C4D4E4C4D4E4C4C4D494D4E4A4E4F4D4F504E4F504E4E4F4D4C4D4B4B4B4B4E4C4B4E4C4B4F4D4C4F4D4C4F4D4C504E4D504E4D504E4D504E4D504E4D504E4D504F4B504F4B504F4B504F4B50504A51514B4D4D474B4B4551504C4C4B474B49485B59586361617B76777E7E7E7A7779847D7A817A77352F221E02025F3616B27B38BB881ACE940CD79507D69302D79507CE8904D08C05D79403D89601D99900D69700D29303CE9004CC9105CC9405C99104CC9105CF9202D19201D39300D49400D49302D49302D29303D19404D29505CC8F00CC8E04C4890EBB861EAC7E26573C0A250D01443D34797973777476837E7B6F6A6B50514F514F4E4A48474E4C4B52504F4E4C4B4E4C4B514F4E4F4D4C504E4D504E4D504E4D504E4D4F4D4C4F4D4C4F4D4C4F4D4C4F50474F50474F4F494F4F494F4F494F4F494F4F494F4F494E4F4B4D4E4A4B4C484A4B474A4B474B4C484C4D494D4E4A4D4E4A4D4E4A4D4E4A4F4E4A4F4E4A4F4E4A4F4E4A4F4E4A4E4E484F4F49504F4B51504C514F4E504E4D4E4C4B4D4B4B4E4C4C4E4C4C4F4D4D4F4D4D504E4E504E4E504E4E504E4E4F4D4C4F4D4C4F4D4C4F4E4A4F4E4A4F4E4A4F4E4A4F4F4950504A4D4D474D4D47504F4B4D4C484B49485856556664647C77787F7F7F7A767B847C7C7F7A79332F241B01015D3616B07B38BB881ACC940DD5940AD49302D59507CC8904CE8D03D59403D69701D99800D69701D29302CE9004CC9105CE9405CB9004CD9102CE9101D39201D39201D39201D39201D49203D29204D19305D29505CC8F00CC8E06C28910B78521AB7D29553C0A230D01433E35787975787577837E7B6D68694E4F4B52504F4B49484E4C4B5351504E4C4B4D4B4A504E4D4F4D4C504E4D504E4D504E4D4F4D4C4F4D4C4F4D4C4F4D4C4F4D4C4D4E4A4D4E4A4D4F494D4F494D4F494D4F494D4F494D4F494E4F4B4D4E4A4B4C484A4B474A4B474B4C484C4D494D4E4A4D50474D50474D50474F50474F50474F5047514F47514F474E4F464F504750504A51504C51504C504E4D4E4C4B4D4B4A4E4C4C4F4D4D4F4D4D4F4D4D504E4E504E4E504E4E504E4E4F4D4C4F4D4C4F4D4C4F4E4A4F4E4A4F4E4A4F4E4A4F4F4950504A4E4E4850504A504F4B4D4C484C4A495654536866667B79788080807B777C837D7E7E797A302E23190101593614AF7B39B9881CCB930ED3930CD29303D59507CA8904CC8D03D59304D69602D79800D69701D29302CE9002CE9203CE9504CD9102CE9002D09101D29100D59201D39201D39102D39102D09204D19305D19506CB8F00CA8E06C08811B68523A97D2A523B0B210D02413E36777B76787577817E7A6C67684C4D495351504C4A494F4D4C5351504E4C4B4D4B4A504E4D4E4C4B504E4D4F4D4C4F4D4C4F4D4C4F4D4C4F4D4C4F4D4C4F4D4C4D4E4A4D4E4A4D4E4A4D4E4A4D4E4A4D4F494D4F494D4F494E4F4B4D4E4A4B4D474A4C464A4C464B4D474C4F464D50474D
 
Upvote 0

Wendell Carneiro Mendes

Member
Licensed User
Longtime User
In my database manager tutorial i did some testing with images from a test database dbdemos.db3.
The blobs in this database were 'old' windows bmp images. I didn't find a solution how to read the blob at the time.
But thanks to @DonManfred suggestion to read the hex dump i found a solution.
Hex string from buffer:
        Dim bc As ByteConverter
        Dim hex As String = bc.HexFromBytes(Buffer)
        Log(hex)
        Dim InputStream1 As InputStream
        If hex.Contains("FFD8FF") Then        ' jpg, jpeg
            InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
        End If
        If hex.Contains("424D") Then        ' bmp
            InputStream1.InitializeFromBytesArray(Buffer, 8, Buffer.Length)
        End If
        If hex.Contains("47494638") Then    ' gif
            InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
        End If
        If hex.Contains("4D4D002A") Or hex.Contains("49492A00") Then    ' tiff
            InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
        End If
        If hex.Contains("89504E470D0A1A0A") Then    ' png
            InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
        End If
When the buffer contains "424D" then the image should be a bmp-file. The "424D" value was found after offset 8.
So when the program reads from offset 8 the image is retrieved and shown.
Try it with your blobs. It might work.
Good afternoon (at least here in Brazil), it worked following this example.
I am very grateful to you Paul Meuris and to Don Manfred.
God bless you all.
 
Upvote 0
Top