Android Question [Solved] Download Image from SQL Server and Display in ImageView

stingrae

Member
Licensed User
Longtime User
Hi again,

I'm not sure where I'm doing this wrong, so will post all the main steps.

In MS SQL Server I have a field defined as a VARBINARY and am storing images in there. This is then retrieved from the REST API and stored in a VB.Net field defined as Byte()

I call the API with the OkHttpItils2 in B4A and it seems to go into bytImageData ok:

B4X:
    Dim job As HttpJob
    job.Initialize("ImageGetByBusiness" , Me)
    job.Download(strURL)
      
    Wait For (job) JobDone(j As HttpJob)

    Dim MyDataList As List
    Dim parser As JSONParser
    Dim response As String = job.GetString
    parser.Initialize(response)
      
    MyDataList.Initialize
      
    Try
      
        MyDataList = parser.NextArray
          
        Dim intID As Int
        Dim intTypeID As Int
        Dim intName As Int
        Dim intDesc As Int
        Dim intData As Int
      
        Dim MapImages As Map
      
        MapImages.Initialize
        For i=0 To MyDataList.Size-1

            MapImages = MyDataList.Get(i)

            ' Get column number first
            If i = 0 Then
                For c = 0 To (MapImages.Size-1)
                    Log( MapImages.GetkeyAt(c))
                    Select Case MapImages.GetkeyAt(c)
                        Case "ImageID"
                            intID = c
                        Case "ImageTypeID"
                            intTypeID = c
                        Case "ImageName"
                            intName = c
                        Case "ImageDesc"
                            intDesc = c
                        Case "ImageData"
                            intData = c
                    End Select
                Next
            End If
              
            Dim intImageID As Int = MapImages.GetValueAt(intID)
            Dim strImageName As String = MapImages.GetValueAt(intName)
            Dim strImageDesc As String = MapImages.GetValueAt(intDesc)
            Dim bytImageData() As Byte = MapImages.GetValueAt(intData)
           
            ' This all works ok  ^^
            ' Code snippets below are from here
                   
        Next
          
    Catch
        Log(LastException)
        Msgbox("Unfortunately there was an error doing the Test.", "Error")
    End Try
  
    j.Release

But then I can't seem to load the image from bytImageData into into an Image View.

This code crashes:
B4X:
            imgTest.Bitmap = BytesToImage(bytImageData)

' Below code pinched from another thread
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)
    Return bmp
End Sub

And this code looks like it works, doesn't change the image:
B4X:
            MapImages.Put(imgTest , bytImageData)

Originally I had a .png file in there but then changed it to a bitmap but same result. I will need PNGs.

Does anyone know where I'm going wrong?

Thanks in advance.
 
Last edited:

stingrae

Member
Licensed User
Longtime User
Found the solution ...! (this forum is so good by the way @Erel :) )

Posting here for others who may experience the same issue ... Just add these lines:

B4X:
Dim strImageData As String = MapImages.GetValueAt(intData)
Dim su As StringUtils
Dim bytImageData() As Byte = su.DecodeBase64(strImageData)                  
imgTest.Bitmap = BytesToImage(bytImageData)
 
Upvote 0
Top