Android Question Error reading blob from sqlite

Aries Abedes

Member
Licensed User
Hi,

I am trying to use the InsertBlob and ReadBlob sub from sqlite tutorial. Inserting works fine but when i am reading it back, it throws these error:

B4X:
Error occurred on line: 209 (aPodDetails)
java.lang.RuntimeException: Error loading bitmap.
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:523)
    at com.emeraldheadway.podreleasing.apoddetails._readblob(apoddetails.java:605)
    at com.emeraldheadway.podreleasing.apoddetails._sql_querycomplete(apoddetails.java:729)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA$2.run(BA.java:338)
    at android.os.Handler.handleCallback(Handler.java:808)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:5387)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:831)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
    at dalvik.system.NativeStart.main(Native Method)

If I use
B4X:
Activity.SetBackgroundImage(bmp)
to load the image, the exception is catch and the application will not exit but the image is still not loaded.

These are the code for these particular activity. Thank you.

B4X:
#Region  Activity Attributes 
    #FullScreen: False
    #IncludeTitle: True
#End Region

#Extends: android.support.v7.app.AppCompatActivity

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    'Private reqManager As DBRequestManager

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 scvPodDetails As ScrollView
    Private pnlPodDetails As Panel
    Private edtAddress As EditText
    Private edtIssDate As EditText
    Private edtPodNo As EditText
    Private edtReceivedBy As EditText
    Private edtSubscriber As EditText
    Private edtSubsNo As EditText
    Private edtTitle As EditText
   
    Private imgSignature As ImageView
    Private sign As SignatureDialogBuilder
   
    Dim bmp As Bitmap
   
    Private btnSave As Button
   
    'Dim imgBuffer() As Byte
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("Layout1")
   
   
    Activity.LoadLayout("lscvPodDetails")
    Activity.Title = "POD DETAILS"
    scvPodDetails.Panel.LoadLayout("lPodDetails")
    scvPodDetails.Panel.Height = pnlPodDetails.Height
    scvPodDetails.Panel.Width = pnlPodDetails.Width
   
   
End Sub



Sub Activity_Resume
    DisableView
    edtReceivedBy.RequestFocus
    Starter.SQl1.ExecQueryAsync("SQL", "SELECT PODID, PODNO, SUBSNO, SUBSCRIBER, ADDRESS, TITLE, ISSUEDATE, RECEIVEDBY, SIGNATURE FROM tblpoddetails WHERE PODID = " & Main.SelectedID, Null)
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

'------------------------------------
'| RESULT FOR EXECQUERYASYNC        |
'------------------------------------
Sub SQL_QueryComplete (Success As Boolean, Cur As Cursor)
    If Success Then
       
        For i = 0 To Cur.RowCount - 1
            Cur.Position = i
            edtPodNo.Text = Cur.GetInt("PODNO")
            edtSubsNo.Text = Cur.GetInt("SUBSNO")
            edtSubscriber.Text = Cur.GetString("SUBSCRIBER")
            edtAddress.Text = Cur.GetString("ADDRESS")
            edtTitle.Text = Cur.GetString("TITLE")
            edtIssDate.Text = Cur.GetString("ISSUEDATE")
                If Cur.GetString("RECEIVEDBY") = Null Then
                    edtReceivedBy.Text = ""
                Else
                    edtReceivedBy.Text = Cur.GetString("RECEIVEDBY")
                End If
        Next
       
        ReadBlob
       
    Else
        Log(LastException)
    End If
   
       

End Sub


'----------------------------------------
'| SIGNATURE                            |
'----------------------------------------
Sub imgSignature_Click
    sign.Show("Signature","PLEASE SIGN HERE!","OK","Cancel",File.DirDefaultExternal,"Signature.png")
    Log(sign)
End Sub

Sub Signature_onSignatureEntered()
    Log($"onSignatureEntered()"$)
    If File.Exists(File.DirDefaultExternal,"Signature.png") Then
        bmp.Initialize(File.DirDefaultExternal,"Signature.png")
        imgSignature.Bitmap = bmp
    End If
End Sub

Sub Signature_onSignatureInputCanceled()
    Log($"onSignatureInputCanceled()"$)
End Sub

Sub Signature_onSignatureInputError(error As String)
    Log($"onSignatureInputError(${error})"$)
End Sub


Sub edtReceivedBy_FocusChanged (HasFocus As Boolean)
    Dim Send As EditText
   
    If HasFocus Then
        Send = Sender
        scvPodDetails.ScrollPosition = Send.Top - 350dip
       
    End If
   
    Dim i As IME
    i.Initialize("")
    i.SetCustomFilter(edtReceivedBy, 0x00001000, " ABCDEFGHIJKLMNOPQRSTUVWXYZ")
End Sub

Sub DisableView
    edtPodNo.Enabled = False
    edtSubsNo.Enabled = False
    edtSubscriber.Enabled = False
    edtAddress.Enabled = False
    edtTitle.Enabled = False
    edtIssDate.Enabled = False
End Sub
   



Sub btnSave_Click
    If edtReceivedBy.Text = " " Then
        Msgbox("Received by cannot be empty.","Input error")
    Else
        Dim Query As String
        Dim DateToday As String
        DateToday = (DateUtils.TicksToString(DateTime.Now))
        Query = "UPDATE tblpoddetails SET  receivedby= '" & edtReceivedBy.Text.Trim & "', receiveddate='" & DateToday & "' WHERE podid = " & Main.SelectedID
        Starter.SQl1.ExecNonQuery(Query)
        InsertBlob
    End If
End Sub

Sub InsertBlob
    'convert the image file to a bytes array
    Dim InputStream1 As InputStream
    InputStream1 = File.OpenInput(File.DirDefaultExternal,"Signature.png")
    Dim OutputStream1 As OutputStream
    OutputStream1.InitializeToBytesArray(2000)
    File.Copy2(InputStream1, OutputStream1)
    Dim Buffer() As Byte 'declares an empty array
    Buffer = OutputStream1.ToBytesArray
      'write the image to the database
    Starter.SQl1.ExecNonQuery("UPDATE tblpoddetails SET signature='" & Array As Object(Buffer) & "' WHERE podid = " & Main.SelectedID )
    'Starter.SQl1.ExecNonQuery2(
End Sub

Sub ReadBlob
    Dim Cur As Cursor
    Cur = Starter.SQL1.ExecQuery("SELECT signature FROM tblpoddetails WHERE podid =" & Main.SelectedID)
    Cur.Position = 0
       
    If Cur.GetBlob("SIGNATURE") <> Null Then
        Dim Buffer() As Byte '= Null 
        Buffer = Cur.GetBlob("SIGNATURE")
        Dim InputStream1 As InputStream
        InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
        bmp.Initialize2(InputStream1)
        'bmp = reqManager.BytesToImage(InputStream1)
        InputStream1.Close
        Try
            imgSignature.Bitmap = bmp
        Catch
            Log(LastException)
        End Try
    End If
       
'    If Buffer = Null Then
'        imgSignature.Bitmap = LoadBitmap(File.DirDefaultExternal,"Signature.png")
'    End If
   
End Sub
 
Top