iOS Question Blob (Image) from a MYSQL using B4i

Cornelius Smit

Member
Licensed User
How do I get a blob (Image) including other fields from an MYSQL using B4i where the image is converted in the SQL statement to BASE64.

example statement
Dim Query As String ="Select `ID`, `Number`,`Address`, IFNULL(`lon`,0) , IFNULL(`lat`,0), 'postal_code`, TO_BASE64(`image`) As image FROM `Emergency`"

Thank you in advance for your help.
 

Cornelius Smit

Member
Licensed User
What is the file size? Try to compress it as a zip file.
I did compress it and it is still too big 1,275 KB (ZIPPED)
I have created the same code in B4j and guess what it is WORKING in b4J
2018-12-19 (4).png

B4J code

I Can not go to sleep tonight( Australia time) as I need to finish this project

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private IMGV As ImageView
    Public DataServer As String = "http://*******"
End Sub
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Main") 'Load the layout file.
    MainForm.Show
    ExecuteQuery_List
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
private Sub ExecuteQuery_List( )
    'ProgressDialogShow("Loading Data...")
        Dim SU As StringUtils
        Dim job As HttpJob
        Dim ListResults As List
        job.Initialize("job",Me)
        Dim Query As String ="Select `ID`, IFNULL(`Type`,'UNKNOWN'), IFNULL(`Number`,0),IFNULL(`Address`,'UNKNOWN'), IFNULL(`lon`,0) , IFNULL(`lat`,0), IFNULL(`postal_code`,0) , IFNULL(TO_BASE64(`image`),'00') As image FROM `Emergency` where `ID`=1"
        'Please note the blob is converted to bas64 in the select statement
        job.PostString(DataServer, Query)
        Wait For (job) JobDone(job As HttpJob)
        If job.Success Then
            Dim parser As JSONParser
            Log(job.GetString)
            parser.Initialize(job.GetString)
            ListResults = parser.NextArray 'returns a list with maps
            If ListResults.Size>0 Then
            Dim M As Map
            M.Initialize
            M= ListResults.Get(0)
            Dim Base64String As String = m.Get("image")
            Dim ByteBuffer() As Byte
            Dim bm As Image
            ByteBuffer=SU.DecodeBase64(Base64String)
            Dim Inputstream1 As InputStream
            Inputstream1.InitializeFromBytesArray( ByteBuffer,0,ByteBuffer.Length)
            bm.Initialize2(Inputstream1)
            IMGV.SetImage(bm) 
             
             
                If job.IsInitialized Then
                    job.Release
                End If
                'ProgressDialogHide
             
            End If
        End If
End Sub
 
Last edited:
Upvote 0

Cornelius Smit

Member
Licensed User
Ok Erel I have pinpointed the base64 decoder as the problem
Dim ByteBuffer() As Byte
ByteBuffer=SU.DecodeBase64(Base64String)
File.WriteBytes(File.DirDocuments,"IOS_base64_BA",ByteBuffer) <---- The buffer stay empty after decoding
please fix stringutils DECODEBASE64
 
Upvote 0

Cornelius Smit

Member
Licensed User
First of all, thank you OliverA for assisting me. The problem was that before decoding you have to replace linefeeds by adding this line before decoding Base64String.Replace(Chr(10),"") and you don't have to do it in B4A or B4J
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Please note that this is just a case where the underlying library/function used by StringUtils's DecodeBase64 differs in the way it handles a Base64 encoded string versus the library/function that is used by B4A/B4J's version. In iOS's case, it looks like you have to set an option in order for the Base64 decoding to ignore the linefeed/newline character (https://developer.apple.com/documentation/foundation/nsdata/base64decodingoptions) that is produced by the DB's TO_BASE64 (in case of MariaDB see https://mariadb.com/kb/en/library/to_base64/).
 
Upvote 0
Top