Android Question MSSQL BLOB 2 Imageview

Hy,
I try to read a Picture from MSSQL Database and Display it to a Imageview.
This code crash the app.

If Bildcode.Text.Length > 0 Then
Dim RS As SD_ResultSet = MSSQL.ExecQuery("SELECT bBild FROM dbo.tBild WHERE kBild='" & Bildcode.Text & "'")
Dim Buffer() As Byte =RS.GetBytes(0)
Dim InputStream1 As InputStream
Do While RS.NextRow
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
Dim Bitmap1 As B4XBitmap = Artikelbild.bitmap
Dim Bitmap1 As Bitmap
Bitmap1.Initialize2(InputStream1)
InputStream1.Close
Artikelbild.SetBackgroundImage(Bitmap1)
Loop
End If

But I also found a Thread with Base64 encode, but it produces the same error ?

'Convert without Dir / File as request #8
Dim bitmap1 As B4XBitmap = ImageView1.Bitmap
Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2(bitmap1)
ImageView2.Bitmap = Base64EncodeDecodeImage.Ba

Error occured on line: 103 (SD-ResultSet)

it looks like in the library, so a error from me...

Anyone can help me ?

Thanks

Steven
 

ilan

Expert
Licensed User
Longtime User
an example project would help a lot to understand your issue.
what is "SD_ResultSet" ?
is this a third party library?
 
Upvote 0
Hy,
I use the Lib [B4X] [B4XLib] SD_SQL (direct access to MySQL, MariaDB, MS SQL, FireBird)

I try to read a Picture from MSSQL Database and Display it to a Imageview.
But this code crash the app.

B4X:
If Bildcode.Text.Length > 0 Then
Dim RS As SD_ResultSet = MSSQL.ExecQuery("SELECT bBild FROM dbo.tBild WHERE kBild='" & Bildcode.Text & "'")
Dim Buffer() As Byte =RS.GetBytes(0)
Dim InputStream1 As InputStream
Do While RS.NextRow
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
Dim Bitmap1 As B4XBitmap = Artikelbild.bitmap
Dim Bitmap1 As Bitmap
Bitmap1.Initialize2(InputStream1)
InputStream1.Close
Artikelbild.SetBackgroundImage(Bitmap1)
Loop
End If

I had seen at other Thread, that a Blob can´t be directly set to Imagecontainer.
These should work with a base64decode, so I tried this...
[B4X] Library Base64 encode/decode Image and File library

B4X:
'Convert without Dir / File as request #8
Dim bitmap1 As B4XBitmap = ImageView1.Bitmap
Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2(bitmap1)
Artikelbild.Bitmap = Base64EncodeDecodeImage.Base64StringToImage(base)

The code I use instead of the Line
B4X:
Artikelbild.SetBackgroundImage(Bitmap1)

I get a error message all the time, Maybe anyone can help me, how to read a BLOB to a Imagecontainer.

Thanks

Steven
 
Upvote 0
Hy,
thanks. My error, I had tried different options. It must be:
B4X:
If Bildcode.Text.Length > 0 Then
Dim RS As SD_ResultSet = MSSQL.ExecQuery("SELECT bBild FROM dbo.tBild WHERE kBild='" & Bildcode.Text & "'")
Dim Buffer() As Byte =RS.GetBytes(0)
Dim InputStream1 As InputStream
Do While RS.NextRow
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
'Dim Bitmap1 As B4XBitmap = Artikelbild.bitmap
Dim Bitmap1 As Bitmap
Bitmap1.Initialize2(InputStream1)
InputStream1.Close
Artikelbild.SetBackgroundImage(Bitmap1)
Loop
End If
But error still there...
 

Attachments

  • Snipaste_2023-06-26_11-13-48.png
    Snipaste_2023-06-26_11-13-48.png
    21.6 KB · Views: 51
Upvote 0
Voila, I used a sample project to test the functions.

B4X:
If Bildcode.Text.Length > 0 Then
Dim RS As SD_ResultSet = MSSQL.ExecQuery("SELECT bBild FROM dbo.tBild WHERE kBild='" & Bildcode.Text & "'")
Dim Buffer() As Byte =RS.GetBytes(0)   <---- ERROR ARE HERE
Dim InputStream1 As InputStream
Do While RS.NextRow
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
'Dim Bitmap1 As B4XBitmap = Artikelbild.bitmap
Dim Bitmap1 As Bitmap
Bitmap1.Initialize2(InputStream1)
InputStream1.Close
Artikelbild.SetBackgroundImage(Bitmap1)
Loop
End If

My full code as attachment. But I think you can reproduce without database connection...
All other SQL querys are working. Only the blob field generate the error message.
 

Attachments

  • aSampleSql.zip
    15.1 KB · Views: 58
Upvote 0

zed

Active Member
Licensed User
Here is an example that works with sqlite.
B4X:
Private Sub insertDB
    Dim q As String
    Dim InputStream1 As InputStream
    InputStream1 = File.OpenInput(File.DirAssets, "as.png")
    Dim OutputStream1 As OutputStream
    OutputStream1.InitializeToBytesArray(1000)
    File.Copy2(InputStream1, OutputStream1)
    Dim Buffer() As Byte =    OutputStream1.ToBytesArray
  
    Try
        q="INSERT INTO test VALUES(?)"
        SQL1.ExecNonQuery2(q, Array As Object(Buffer))
    Catch
        Log(LastException)
    End Try 
      
    loadImg 
 
End Sub

Private Sub loadImg
  
    Dim RS As ResultSet = SQL1.ExecQuery("SELECT * FROM test")
    RS.NextRow

    Dim Buffer() As Byte  = RS.GetBlob("myblob")
    Dim InputStream1 As InputStream
    InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
    Dim Bmp As Bitmap
    Bmp.Initialize2(InputStream1)
    InputStream1.Close
    ImageView1.Bitmap = Bmp
End Sub

How was the Blob inserted into the database.
The problem may come from there.

If the blob is badly saved, when you want to recover it, the resultset will be 0
 

Attachments

  • testBlob.zip
    16.5 KB · Views: 64
Last edited:
Upvote 0
Hello,
the problem seems to be related to just this one line:


B4X:
Dim Buffer() As Byte =RS.GetBytes(0)

The Database are MSSQL 2019 which is generated from JTL (merchandise management system).

I had found there at the forum these PHP code to show a Image from the database (blob) output:

PHP:
echo '<img src="data:image/jpg;base64,'.base64_encode($row['bVorschauBild']).'" title="" alt="" />';

So maybe I need to encode the output, but my problem are before this step.

I can`t read the value out of the database...
 
Upvote 0
Top