B4J Question How to display in a webview images stored into MySql DB

lucdrb

Active Member
Licensed User
Longtime User
HI,

I've a MySql database where I store images in blob format. The images are from different users.
I've to mix text and images in the final result, for this purpose I thinking of using a webview.

The text should be create with an HTMLEditor.
But for the images from the database, I don't have any idea to display it in the webview.

Your help should be welcome.

Luc
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Hello!

to store the images in mysql

B4X:
private Sub ImageToBlob(dir As String, f As String) As String
        Dim InputStream1 As InputStream
        InputStream1 = File.OpenInput(dir,f)
      
        Dim su As StringUtils
      
        Dim OutputStream1 As OutputStream
        OutputStream1.InitializeToBytesArray(1000)
        File.Copy2(InputStream1, OutputStream1)
        Dim Buffer() As Byte 'declares an empty array
        Buffer = OutputStream1.ToBytesArray
        Return su.EncodeBase64(Buffer)
End Sub

'use it like this:
SQLInterno.ExecNonQuery2("INSERT INTO table (text, image) VALUES (?,?)",Array("test",ImageToBlob(File.DirApp,"name")))

and retrieve them like this:

B4X:
public Sub blobToIO(bySt As String) As InputStream
    Dim su As StringUtils
    Dim by() As Byte = su.DecodeBase64(bySt)
  
    Dim InputStream1 As InputStream
    InputStream1.InitializeFromBytesArray(by, 0, by.Length)
'    InputStream1.Close  
    Return InputStream1
End Sub

'and use it like this:
    Dim r As Image
    r.Initialize2(sql1.execquerysingleresult("SELECT image FROM table WHERE text = 1")
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

lucdrb

Active Member
Licensed User
Longtime User
Thanks Enrique
But I'm interrested in the next step:

Display the image in a webview.

Luc
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Save the image to a file and then show it like you show any image file.

B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Private WebView1 As WebView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.RootPane.LoadLayout("1") 'Load the layout file.
   MainForm.Show
   WebView1.LoadHtml($"
   <h1>Title</h1>
   <img src="${File.GetUri("c:\temp", "1.JPG")}"/>
   
"$)
   
End Sub
 
Upvote 0
Top