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
Why do you use base64 string? The string can be huge.

The best way to do it is with jRDC2.
I only because used it in the Android version using a PHP web service hosted by Godaddy, The image is saved on the mobile device in a map with other data for offline retrieval.
please advice.
 
Last edited:
Upvote 0

Cornelius Smit

Member
Licensed User
Hi Erel
How do I implement jRDC2 on shared hosting? And I get unrecoverable errors on B4I when using Using Images from a blob
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Cornelius Smit

Member
Licensed User
Not possible. You need a VPS.


I'm sorry but I do not provide private technical support.


Ok.

This is all that I can say with the information you provided.
It's just sad that I tried to explain the issue, I don't need technical support. The code in Android work 100% but not in B4I I thought that B4x Is cross-platform and just wanted to send it to you so that you can verify if there are issues with the platform I know my code is working on android I was so excited at first but now it's a deferent story. you want me to open a thread for every issue in the meantime it is the same issue with multiple results ((((
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
How are you accessing MySQL from Android? What library are you using? What is not working in B4i that works in B4A?
 
Upvote 0

Cornelius Smit

Member
Licensed User
How are you accessing MySQL from Android? What library are you using? What is not working in B4i that works in B4A?

I am using HTTPUTILS2 (to consume php web-service)
I Can retrieve normal field data the problem is when this line is executed it just closes
bm.Initialize2(Inputstream1)

(THIS CODE RUN WITHOUT ERRORS IN B4A SAME DATABASE AND PICTURES)

B4X:
'Code module
#Region  Project Attributes
    #ApplicationLabel: Emeralds
    #Version: 1.0.0
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
    #Target: iPhone, iPad
    #ATSEnabled: True
    #MinVersion: 7
#End Region
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private CLV As CustomListView
    Private lblInfo_Insert_1 As Label
    Private img_Insert_1 As ImageView
    Private btn_Insert_1 As Button
    Dim Data As DataClass
End Sub
Private Sub Application_Start (Nav As NavigationController)
    'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
    NavControl = Nav
 
    Page1.Initialize("Main")
    Page1.RootPanel.LoadLayout("Main")
    Page1.Title = "Emerald QLD"
    Page1.RootPanel.Color = Colors.Blue
 
    NavControl.ShowPage(Page1)
 
 
 
    CLV.AddTextItem("EMERGINCY SERVICES","")
    Data.Initialize
    wait for (Data.GetEmergincyList)Complete(bList As List)
If bList.IsInitialized=True Then
    For I = 0 To bList.Size-1
   
 
        Dim M As Map
    M.Initialize
    M=bList.get(I)
            Log(M)
            File.WriteMap(File.DirDocuments,"imagemap",M)
       
    Dim SU As StringUtils
    Dim bm1 As Bitmap
            bm1.Initialize(File.DirAssets,"emeraldLogo.png")
       
            Dim Base64String As String = m.Get("image")
       
            Dim ByteBuffer() As Byte
            Dim bm As Bitmap
            If Base64String.Length > 4 Then
                File.WriteString(File.DirDocuments,"Base64File.txt",Base64String)
                ByteBuffer=SU.DecodeBase64(Base64String)
                Dim Inputstream1 As InputStream
                Inputstream1.InitializeFromBytesArray( ByteBuffer,0,ByteBuffer.Length)
           
                'File.Copy2( File.OpenInput(File.DirDocuments,"aimg.jpg"),Inputstream1)
                bm.Initialize2(Inputstream1)
           
           
           
           
                Inputstream1.Close
                Sleep(100)
            Else
                bm.Initialize(File.DirAssets,"emeraldLogo.png")
            End If
 
    If bm.IsInitialized=True Then
    CLV.Clear
                CLV.Add(CreateListItem(bm,m.Get("Type") & CRLF & m.Get("Number") ,"2"),200,2)
    End If
    Next
    CLV.AddTextItem("BUSINESS","")
    CLV.AddTextItem("TOURISM","")
 
    End If
End Sub
Sub CreateListItem(IMAGE As Bitmap, TEXT As String, itemNum As String) As Panel
Dim p As Panel
p.Initialize("")
p.LoadLayout("CLV_Insert_1")
p.Color = Colors.White
img_Insert_1.Bitmap=IMAGE
lblInfo_Insert_1.Text=TEXT
Return p
End Sub
Private Sub Page1_Resize(Width As Int, Height As Int)
 
End Sub
Private Sub Application_Background
 
End Sub
Sub btn_Insert_1_Click
 
End Sub
Sub img_Insert_1_Click
 
End Sub
Sub lblInfo_Insert_1_Click
 
End Sub


and my data class

B4X:
Sub GetEmergincyList() As ResumableSub
    Try
        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`"
        '"SELECT `ID`, `category`, TO_BASE64(`image`) as image FROM `business_category`"    TO_BASE64(`image`) as image,
        wait for (ExecuteQuery_List(Query))complete(lList As List)
        If lList.IsInitialized=True Then
            Return lList
        End If
    Catch
        hud.ProgressDialogHide
        Log(LastException.Message)
    End Try
End Sub

private Sub ExecuteQuery_List(Query As String ) As ResumableSub
    hud.ProgressDialogShow("Loading Data...")
    Try
        Dim job As HttpJob
        Dim ListResults As List
        job.Initialize("job",Me)
        job.PostString(Global.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
                If job.IsInitialized Then
                    job.Release
                End If
                hud.ProgressDialogHide
                Return ListResults
            Else
                If job.IsInitialized Then
                    job.Release
                End If
                hud.ProgressDialogHide
                Return  ListResults
            End If
        Else
            Log(job.ErrorMessage)
            If job.IsInitialized Then
                job.Release
            End If
            hud.ProgressDialogHide
            Return ListResults
        End If
 
    Catch
        hud.ProgressDialogHide
        Log(LastException.Message)
        Return Null
    End Try
End Sub
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
How are you storing the image to the database in B4A?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Show us the B4A code that takes the image, coverts it to a bytearray and then transforms the array to base64.
 
Upvote 0

Cornelius Smit

Member
Licensed User
Show us the B4A code that takes the image, coverts it to a bytearray and then transforms the array to base64.

This is the code for B4A (WORKING)

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public DataServer As String = "http://webforce.app*******"
End Sub
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
 
    Private IMGV As ImageView
End Sub
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Main")
 
End Sub
Sub Activity_Resume
ExecuteQuery_List
End Sub
Sub Activity_Pause (UserClosed As Boolean)
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 Bitmap
            ByteBuffer=SU.DecodeBase64(Base64String)
            Dim Inputstream1 As InputStream
            Inputstream1.InitializeFromBytesArray( ByteBuffer,0,ByteBuffer.Length)
            bm.Initialize2(Inputstream1)
            IMGV.Bitmap=bm
           
           
                If job.IsInitialized Then
                    job.Release
                End If
                ProgressDialogHide
           
            End If
        End If
End Sub





This is the code for B4i (NOT WORKING) although simular code
The B4i Just Craches when bm.Initialize2(Inputstream1)

B4X:
'Code module
#Region  Project Attributes
    #ApplicationLabel: B4i Example
    #Version: 1.0.0
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
    #Target: iPhone, iPad
    #ATSEnabled: True
    #MinVersion: 7
#End Region
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private IMGV As ImageView
    Public DataServer As String = "https://webforce.app*******"
    Dim HUDD As HUD
End Sub
Private Sub Application_Start (Nav As NavigationController)
    'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    NavControl.ShowPage(Page1)
    Page1.RootPanel.LoadLayout("Page1")
    ExecuteQuery_List
End Sub
Private Sub Page1_Resize(Width As Int, Height As Int)
 
End Sub
Private Sub Application_Background
 
End Sub
private Sub ExecuteQuery_List( )
    HUDD.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 Bitmap
            ByteBuffer=SU.DecodeBase64(Base64String)
            Dim Inputstream1 As InputStream
            Inputstream1.InitializeFromBytesArray( ByteBuffer,0,ByteBuffer.Length)
            bm.Initialize2(Inputstream1)  '<---- Crash Here
            IMGV.Bitmap=bm
           
           
            If job.IsInitialized Then
                job.Release
            End If
            HUDD.ProgressDialogHide
           
        End If
    End If
End Sub



The Images are there to show the libraries loaded
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
How are you storing the image data in the database? You have shown the retrieval of the blob data, but I want to know how you store the image data.
 
Upvote 0

Cornelius Smit

Member
Licensed User
How are you storing the image data in the database? You have shown the retrieval of the blob data, but I want to know how you store the image data.



Ok, it is a long blob I have another PHP program that I use to upload and save the image in its original format that is PNG.
If there is a way that I can send you the username and password securely, you can open the app. The app was constructed using phpmaker from HKVStore
 
Last edited:
Upvote 0

Cornelius Smit

Member
Licensed User
I Also compared the string between B4A and B4X and it is exactly the same. According to me everything I used should be compatible
 
Upvote 0

Cornelius Smit

Member
Licensed User
You need to upload a text file with the base 64 string and you need to post the error message.

The file is too large for upload.

J'ai essayé de télécharger le fichier mais il est trop gros

Error occurred on line: 68 (Main)
Error loading image.
Stack Trace: (
CoreFoundation <redacted> + 256
libobjc.A.dylib objc_exception_throw + 60
CoreFoundation <redacted> + 0
B4i Example -[B4IBitmap innerInit::] + 352
B4i Example -[B4IBitmap Initialize2:] + 76
B4i Example -[ResumableSub_main_ExecuteQuery_List resume::] + 4052
CoreFoundation <redacted> + 148
CoreFoundation <redacted> + 308
B4i Example +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1624
B4i Example -[B4IShell runMethod:] + 448
B4i Example -[B4IShell raiseEventImpl:method:args::] + 1640
B4i Example -[B4IShellBI raiseEvent:eventarams:] + 1372
B4i Example -[B4IDelegatableResumableSub resume::] + 380
B4i Example -[B4I checkAndRunWaitForEvent:eventarams:] + 552
B4i Example -[B4IShellBI raiseEvent:eventarams:] + 1228
B4i Example -[B4ICommon CallSub4::::] + 344
B4i Example -[B4ICommon CallSub2::::] + 360
B4i Example -[b4i_httpjob _complete:] + 188
B4i Example -[b4i_httputils2service _completejob::::] + 412
B4i Example -[b4i_httputils2service _hc_responsesuccess::] + 108
CoreFoundation <redacted> + 148
CoreFoundation <redacted> + 308
B4i Example +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1624
B4i Example -[B4I raiseEvent:eventarams:] + 564
B4i Example __61-[B4IHttp URLSession:downloadTask:didFinishDownloadingToURL:]_block_invoke + 268
libdispatch.dylib <redacted> + 20
libdispatch.dylib <redacted> + 96
libdispatch.dylib <redacted> + 20
libdispatch.dylib _dispatch_main_queue_callback_4CF + 1016
CoreFoundation <redacted> + 16
CoreFoundation <redacted> + 1912
CoreFoundation CFRunLoopRunSpecific + 452
GraphicsServices GSEventRunModal + 104
UIKitCore UIApplicationMain + 216
B4i Example main + 124
libdyld.dylib <redacted> + 4
)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…