Android Question Problems in extracting a SQLite record

uniplan

Active Member
Licensed User
Longtime User
I put a picture of 2 Mb in a Blob field in a database SQlite.
I manage to correctly insert the record.
However when I try to remove it with the following code:
B4X:
Dim Buffer() As Byte 'declare an empty byte array
Buffer = cursor1.GetBlob(nome_campo)

I have this error:

java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

There is a limit on the size of the blob field?
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
The bitmaps come from a file where I save a photo by this code:

B4X:
    Dim filename As String = tempImageFile
    Dim dir As String = imageFolder
    cameraACL.StartPreview
   
    Dim out As OutputStream
    out = File.OpenOutput(dir, filename, False)
    out.WriteBytes(Data, 0, Data.Length)
    out.Close

Then I use this code for inserting:

B4X:
 Dim InputStream1 As InputStream
InputStream1 = File.OpenInput(imageFolder, tempImageFile)
Dim dimensione As Int = InputStream1.BytesAvailable
Dim OutputStream1 As OutputStream
OutputStream1.InitializeToBytesArray(1000)
File.Copy2(InputStream1, OutputStream1)
Dim Buffer() As Byte 'declares an empty array
Buffer = OutputStream1.ToBytesArray

Dim nomefile As String = fg.get_data_str(DateTime.Now,"yyyyMMddHHmmss") & ".jpg"
Dim didascalia As String = edtMANallDidascalia.Text & ""
          
      
        'write the image to the database
        str_sql = "insert into man_straordinarie_allegati (id_man_straordinarie,chiave_attivazione,str_conn_slave,id_azienda_slave,"
        str_sql = str_sql & "id_automezzi,allegato,estensione,nomefile,dimensione,didascalia) "
        str_sql = str_sql & " values "
        str_sql = str_sql & " (?,?,?,?,"
        str_sql = str_sql & "?,?,?,?,?,?) "
       
        Main.SQL1.ExecNonQuery2(str_sql, Array As Object(man_str_attiva.id_man,app_cfg.chiave_attivazione, _
        di.automezzo_corrente.str_conn,di.automezzo_corrente.id_azienda_slave, _
        di.automezzo_corrente.id_automezzo,Buffer,".jpg",nomefile,dimensione,didascalia))
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your complete code can be written like this:
B4X:
Dim Buffer() As Byte = Bit.InputStreamToBytes(File.OpenInput(imageFolder, tempImageFile))
Dim nomefile As String = fg.get_data_str(DateTime.Now,"yyyyMMddHHmmss") & ".jpg"
Dim didascalia As String = edtMANallDidascalia.Text & ""
'write the image to the database
str_sql = $"insert into man_straordinarie_allegati (id_man_straordinarie,chiave_attivazione,str_conn_slave,id_azienda_slave,
   id_automezzi,allegato,estensione,nomefile,dimensione,didascalia)
   values (?,?,?,?,?,?,?,?,?,?) "$
You will need to resize the images. You can do it at runtime by using LoadBitmapSample. However it is better to resize them before they are added to the project.
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
I tried to save an image with the following code

B4X:
    cameraACL.StartPreview
    Dim out As OutputStream
    out = File.OpenOutput(dir, filename, False)
    out.WriteBytes(Data, 0, Data.Length)
    out.Close

I have found that the saved image size is different depending on if you use a Samsung S6 or a Huawei P9.

Depends on what?
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
I have tried to set the picturesize in Camera1_Ready event with this code:

B4X:
Sub Camera1_Ready (Success As Boolean)
    Try
        If Success Then
            cameraACL.OriPortrait
            cameraACL.PictureSize(320,480)
            cameraACL.StartPreview
           
           
        Else
            ToastMessageShow("Non posso aprire la camera.", True)
        End If
       
    Catch
        LogColor(LastException,Colors.Blue)
    End Try
   
End Sub

But I have this error:
(NullPointerException) java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.reconnect()' on a null object reference
Error occurred on line: 3490 (frmMainAutista)
java.lang.RuntimeException: setParameters failed
at android.hardware.Camera.native_setParameters(Native Method)
at android.hardware.Camera.setParameters(Camera.java:2624)
at xvs.ACL.ACL.PictureSize(ACL.java:572)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:753)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:343)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA$2.run(BA.java:328)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5665)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
(Exception) java.lang.Exception: java.lang.RuntimeException: setParameters failed
 
Upvote 0
Top