Android Question convert byte to string

FormCommander

Member
Licensed User
Longtime User
Hello,
I store a XML with base64 encoded images into a SQL blob field and want to read it back into a string for parsing. But I get an error
Cannot cast type {Type=Byte,Rank=0,ReomoteObject=True}to:{Type=Byte,Rank=1,RemoteObject=True}


B4X:
Dim bc As ByteConverter
             Dim cBytes As Byte
            cBytes = Cursor1.GetBlob("XML")
             Dim cXML As String = bc.StringFromBytes(cBytes)

So what I am doing wrong with the conversation ?
And I am right that it is possibe to parse the base64 encoded image fields from the XML back with Xml2Map or Sax ?
 

udg

Expert
Licensed User
Longtime User
You defined cbytes as a single byte
 
Upvote 0

FormCommander

Member
Licensed User
Longtime User
Hello,
thank you for your answers, I do it the way Erel posted. But now I get an sql error. The XML I store in the blob field is correct, I can open it in any other viewer like XM notepad. Also the other fields are no problem. What can be wrong ?
regards
Rudolf


B4X:
Dim Cursor1 As Cursor
Cursor1 = Main.SQLobj.ExecQuery("SELECT ID,XML,BARCODE,OBJECT FROM document WHERE BARCODE = '" & Main.cBarCode & "'" )
For i = 0 To Cursor1.RowCount - 1
      Cursor1.Position = i
      Dim bytes() As Byte = Cursor1.GetBlob("XML") 'Line 41



Error occurred on line: 41 (doclist)
java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
    at android.database.CursorWindow.nativeGetBlob(Native Method)
    at android.database.CursorWindow.getBlob(CursorWindow.java:416)
    at android.database.AbstractWindowedCursor.getBlob(AbstractWindowedCursor.java:45)
    at anywheresoftware.b4a.sql.SQL$CursorWrapper.GetBlob(SQL.java:422)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.samples.camera.doclist.afterFirstLayout(doclist.java:102)
    at anywheresoftware.b4a.samples.camera.doclist.access$000(doclist.java:17)
    at anywheresoftware.b4a.samples.camera.doclist$WaitForLayout.run(doclist.java:80)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6776)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
 
Upvote 0

FormCommander

Member
Licensed User
Longtime User
Hello Erel,
thank you, have checked the database with ODBC on my Windows computer, no problem to open a cursor and retrieve the XML. I have changed my code as recommended in you excellent video, I think this is all correct now. But get still an error. I will try tp make a little B4J program to try to get the XML fields. The size of the blob is smaller than 1 mb, is not more possible or problems expectec ?
regards
Rudolf

error.jpg
 
Upvote 0

FormCommander

Member
Licensed User
Longtime User
Hello, found that one image was a little bit more than 1 MB, it seens that I can store the image, but then it is not possible to open an cursor on this table. I will store the images in files instead of the sql table.
Thank you all for your help
regards
Rudolf
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
images in files instead of the sql table
It is a bad practice to store images in the Database. In SQlite it is mandatory not to store big images as you just found out.
Store it on Disc and only store the filename in the Database.
 
Upvote 0
Top