B4J Question File chooser for image pick

microbox

Active Member
Licensed User
Longtime User
I'm trying to construct code that will use file chooser to open an image file (jpg) in preparation to save it to Mysql database as blob file. The code I'm experimenting is a copy and paste from different thread.
B4X:
Sub btnfd_MouseClicked (EventData As MouseEvent)
    Dim f As String = fc.ShowOpen(MainForm)
    If f <> "" Then
        TextArea1.Text = File.ReadString(f, "")
    End If
  
    For Each row() As Object In result.Rows
    Dim out As OutputStream = File.OpenInput("C:\AB4JProjects","caption.jpg")
    Dim In As InputStream
    Dim Buffer() As Byte
    Buffer = row(0) ' gives error here, says that can not convert a string to byte array
    In.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
    File.Copy2(In, out)
    out.Close
End Sub

1. I don't really understand, how result.rows be used here?
2. How can I replace File.OpenInput("C:\AB4JProjects","caption.png") after I select using File chooser?

I appreciate any input and thanks for time.
 

EnriqueGonzalez

Expert
Licensed User
Longtime User
microbox, please delete your previews attempts, because you will have problems parsing the images if they were created with your prevous method
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
microbox, please delete your previews attempts, because you will have problems parsing the images if they were created with your prevous method
Okay...I modified the code to Dim r() As Object = result.rows.get(0)
but gives error to this line..
B4X:
im.Initialize2(blobToIO(r(1)))
This is the error
B4X:
Waiting for debugger to connect...
Program started.
Select Table Query triggered
Done here
Tag: select, Columns: 3, Rows: 4
id    descript    image   
2    Test1    [B@2251fcf3   
5    Test1    [B@33dd5b61   
6    Test1    [B@494c5d8e   
7    Test1    [B@656ca9af   
Error occurred on line: 126 (Main)
java.lang.ArrayIndexOutOfBoundsException: 1
    at b4j.example.main._reqmanager_result(main.java:375)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:614)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:480)
    at anywheresoftware.b4a.keywords.Common.access$0(Common.java:460)
    at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:534)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
 
Upvote 0

EnriqueGonzalez

Expert
Licensed User
Longtime User
You are almost there.

Delete your previous attempts, if you are getting all the columns (as you are) look which one is the image and assign the index to r

B4X:
Dim r() as object = result.rows.get(0) 'if you deleted your previous attempts 

r(2) ' if your image column is your third
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
You are almost there.

Delete your previous attempts, if you are getting all the columns (as you are) look which one is the image and assign the index to r

B4X:
Dim r() as object = result.rows.get(0) 'if you deleted your previous attempts
r(2) ' if your image column is your third

I did just that Enrique, but bumped to another error
B4X:
Waiting for debugger to connect...
Program started.
Select Table Query triggered
Error here
Done here
Tag: select, Columns: 3, Rows: 4
id    descript    image  
2    Test1    [B@709a2a9d  
5    Test1    [B@6e4f62ad  
6    Test1    [B@343a2e93  
7    Test1    [B@3ac8e17b  
Open Triggered!
[B@1ba7fa7d
Error occurred on line: 79 (Main)
java.io.IOException: Bad Base64 input character decimal 91 in array position 0
    at anywheresoftware.b4a.objects.Base64.decode(Base64.java:1204)
    at anywheresoftware.b4a.objects.Base64.decode(Base64.java:1259)
    at anywheresoftware.b4a.objects.Base64.decode(Base64.java:1227)
    at anywheresoftware.b4a.objects.StringUtils.DecodeBase64(StringUtils.java:36)
    at b4j.example.main._blobtoio(main.java:143)
    at b4j.example.main._reqmanager_result(main.java:384)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:614)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:480)
    at anywheresoftware.b4a.keywords.Common.access$0(Common.java:460)
    at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:534)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

Error occurred on line 79 (Main)
B4X:
public Sub blobToIO(bySt As String) As InputStream
    Dim su As StringUtils
    Dim by() As Byte = su.DecodeBase64(bySt) ' ----- This is line 79 here
    Log("Count: " & by.Length)
    Dim InputStream1 As InputStream
    InputStream1.InitializeFromBytesArray(by, 0, by.Length)
    InputStream1.Close
    Return InputStream1
End Sub
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
@enrique I'm still having trouble displaying the image..I'll try another way perhaps. Thanks for the effort and being friendly BTW I sent something for your coffee (from jsat550.y.com) :)
 
Upvote 0

EnriqueGonzalez

Expert
Licensed User
Longtime User
May be we have to step by step, because i am concerned on your logs.

for example:

Error here

this says that you have an error somewhere, so how does it look in your db when you store the image? it has to be something like:

as you can see the image has a very specific pattern.
upload_2017-5-26_11-17-47.png
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
Hmm looks different, Is it okay if I try your demo project from here?
s4.png
 
Last edited:
Upvote 0

EnriqueGonzalez

Expert
Licensed User
Longtime User
Yes, we can go step by step and see where the error is.

my questions are:

your JRDC "insert" is like this?
INSERT INTO Table1(description, image) VALUES (?,?)

is this code storing your image on the db?

if that is so, what id is giving to it?

using this code only.
B4X:
Sub btnfile_MouseClicked (EventData As MouseEvent)
'------------------------------------------------------------------------------
Dim fc As FileChooser
    fc.Initialize
    fc.Title="Select Photo"
    fc.InitialDirectory = "C:\"
    fc.SetExtensionFilter("Image",Array As String("*.jpg","*.png","*.bmp"))
  
    Dim f As String = fc.ShowOpen(MainForm)
    If f <> "" Then
        Dim BlobedImage As String = ImageToBlob(f)
        '------------------------------------------------------------------------
        Dim cmd As DBCommand
        cmd.Initialize
        'Defined as sql.insert in the RDC file config.properties located in the files folder
        cmd.Name = "insert"
        'Set the parameter defined as sql.insert=INSERT INTO notes (description, content) VALUES(?, ?)
        cmd.Parameters = Array As Object("Test1", BlobedImage)
        'Trigger the command. The result is handled by JobDone.
        'reqManager.ExecuteCommand(cmd, "insert")
        Log(BlobedImage)
        Log("Insert table Triggered")
    End If
'------------------------------------------------------------------------------------------- 
End Sub

private Sub ImageToBlob(FileChooserResult As String) As String
    Dim InputStream1 As InputStream
  
    Dim directory As String =FileChooserResult.SubString2(0,FileChooserResult.LastIndexOf("\"))
    Dim FileName As String = FileChooserResult.SubString(FileChooserResult.LastIndexOf("\") + 1)
    InputStream1 = File.OpenInput(directory,FileName)
    Log(directory & " Direct") 
    Log(FileName & " File") 
    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
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
Good morning Enrique, I created a new database and table. I'm using the code you mentioned and this is the results on my IDE and in PhpMyAdmin.
forum111.png

Are you referring to the id field? (generated as auto increment)
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
The image's actual size is 3.23KB
I used blob as data type.
Thanks for the link.
 

Attachments

  • img2.jpg
    img2.jpg
    3.2 KB · Views: 237
Last edited:
Upvote 0

EnriqueGonzalez

Expert
Licensed User
Longtime User
okey, i sry hope this works!

B4X:
Sub btnOpen_MouseClicked (EventData As MouseEvent)
    Dim cmd As DBCommand
    cmd.Initialize
    'Defined as sql.select2 in the RDC file config.properties located in the files folder
    cmd.Name = "select1"
    Dim id As Int = 1
    cmd.Parameters = Array As Object(id)
    'Trigger the query. The result is handled by JobDone.
    reqManager.ExecuteQuery(cmd, 0, "select1")
End Sub

Sub JobDone(Job As HttpJob)
   If Job.Success = False Then
     Log("Error: " & Job.ErrorMessage)
   Else
     If Job.JobName = "DBRequest" Then
        reqManager.HandleJobAsync(Job, "ReqManager")
     End If
   End If
   Job.Release
End Sub

Sub ReqManager_Result(result As DBResult)
   'reqManager.PrintTable(result)
'   reqManager.PrintTable(result)
    If result.Tag = "select1" Then
        Dim r() As Object = result.rows.get(0)
        Dim byStr As String = r(2)
        Log(byStr)
        Dim im As Image
        im.Initialize2(blobToIO(byStr))'The column index of your query.

        ImageView1.SetImage(im)
    End If
    Log("Done here")
   'reqManager.PrintTable(result)
End Sub

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
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
Almost..but gives out an error
B4X:
java.io.IOException: Bad Base64 input character decimal 91 in array position 0
pointing to this line (130)
B4X:
Dim by() As Byte = su.DecodeBase64(bySt) ' --line 130
This is what I have in the config.properties
B4X:
sql.select=select * from table1
sql.insert=INSERT INTO table1(description, image) VALUES (?,?)
sql.select1=select * FROM table1 WHERE id = ?
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
okay..
B4X:
[B@3dcc4bcd before blobToIO function 
[B@3dcc4bcd in blobToIO function --
Error occurred on line: 131 (Main)
java.io.IOException: Bad Base64 input character decimal 91 in array position 0
    at anywheresoftware.b4a.objects.Base64.decode(Base64.java:1204)
    at anywheresoftware.b4a.objects.Base64.decode(Base64.java:1259)
    at anywheresoftware.b4a.objects.Base64.decode(Base64.java:1227)
    at anywheresoftware.b4a.objects.StringUtils.DecodeBase64(StringUtils.java:36)
    at b4j.example.main._blobtoio(main.java:146)
    at b4j.example.main._reqmanager_result(main.java:391)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method).........
B4X:
Sub ReqManager_Result(result As DBResult)
   'reqManager.PrintTable(result)
    If result.Tag = "select1" Then
        Dim r() As Object = result.rows.get(0)
        Log("r " & r(2))
        Dim byStr As String = r(2)
        Log(byStr & " before blobToIO function")
        Dim im As Image
        im.Initialize2(blobToIO(byStr))'The column index of your query.

        ImageView1.SetImage(im)
    End If
    Log("Done here")
   'reqManager.PrintTable(result)
End Sub

public Sub blobToIO(bySt As String) As InputStream
    Log(bySt & " in blobToIO function --")
    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
 
Upvote 0
Top