Android Question How to read blob field from SQL Server and display bitmap in Imageview ?

sukudxb

Member
Licensed User
Longtime User
Hi,

I need to read an image filed from Sql Server database and show the image in ImageView. Can any body help me please. I have created a web service to read data from the server. I am getting all other fields without any problem, but I don't have any idea how to convert the image field to bitmap and display it in image view.

I am using HttpUtils to read data from Sql Server, and the data is returned to me as JSON String. Anybody please help me.
 

NFOBoy

Active Member
Licensed User
Longtime User
I use similar to this for audio files, should work for your images?

B4X:
    Dim cursor1 As Cursor
  
'need to get the blob into a cursor
cursor1 = SQL1.ExecQuery("Put Query Here to get your blob'")
    cursor1.position = 0
'now need to get the data from the cursor into a Buffer
    Dim Buffer() As Byte 'declare an empty byte array

'move the cursor info into the Buffer
    Buffer = cursor1.GetBlob("Insert Name of the field that has your blob in it here")
'get the Buffer into a Stream
    Dim ImageInStream As InputStream
    ImageInStream.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
'  ImageInStream holds the data.. need to get it into a bitmap
  
    Dim iv As ImageView
    Dim thisBitmap As Bitmap
    iv.Initialize("iv")
    thisBitmap.Initialize2(ImageInStream)
    iv.Bitmap = thisBitmap
'close the stream
ImageInStream.Close
 
Upvote 0

Gabino A. de la Gala

Well-Known Member
Licensed User
Longtime User
I'm trying like this:

B4X:
Dim ListOfMaps As List
                Dim b() As Byte
                Dim s As StringUtils
               
                ListOfMaps = parser.NextArray 'returns a list with maps
                If ListOfMaps.Size > 0 Then
                        ' Inicializo la copia de los datos locales...
                        InicializarTablaLocal(Main.LISTA_FOTOS)
                       
                        For i = 0 To ListOfMaps.Size -1
                            ProgressBar1.Progress=Round(ListOfMaps.Size / i+1)
                            Dim mapaAux As Map
                            mapaAux = ListOfMaps.Get(i)
                            b = s.DecodeBase64(mapaAux.Get("FOTO"))
                            mapaAux.Put("FOTO", BytesToString(b, 0, b.Length, "iso-8859-1"))
                        Next
                       
                        ' Inserto los datos obtenidos del servidor para la copia local
                        DBUtils.InsertMaps(Sql1, "FOTOS", ListOfMaps)
                        Label2.Text = ListOfMaps.Size
                        ToastMessageShow("FOTOS Insertadas: " & (ListOfMaps.Size), True)
                        MenuSincronizar.FotosImportadas = ListOfMaps.Size
                    Else
                        ToastMessageShow("Error: " & Job.ErrorMessage, True)
                        ToastMessageShow("Error: No se obtuvieron registros", True)
                End If

But I think, it doesn't work correctly.

Can anybody help me?

Sorry for my poor English. I'm Spanish.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…