0

Hi I want to store all camera capturing images directly to the sqlite database in basic4android. I have created database and table also. Please tell me how I can insert all images to the sqlite database? Please help me.

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
    sql1.Initialize(File.DirDefaultExternal,"avi.db",True)
End If 
CreateTables

    Activity.LoadLayout("1")
    timer1.Initialize("timer1","2500")
    timer1.Enabled=True
End Sub
Sub CreateTables

     sql1.ExecNonQuery("DROP TABLE IF EXISTS TABLE1")
     sql1.ExecNonQuery("CREATE TABLE TABLE1(IMAGE BLOB)")


End Sub 

Sub Camera1_Ready (Success As Boolean)
If Success Then
    camera1.StartPreview
    btnTakePicture.Enabled = False
Else
    ToastMessageShow("Cannot open camera.", True)
End If
End Sub

Sub Activity_Resume
    btnTakePicture.Enabled = False
    camera1.Initialize(Panel1, "Camera1")
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    camera1.Release
End Sub

Sub Camera1_PictureTaken (Data() As Byte)
    camera1.StartPreview
    DateTime.DateFormat ="HH.mm.ss.SS_dd-MM-yy_"
    forDate=DateTime.Date(DateTime.now)
    imei = pID.GetDeviceId
    filename = forDate & imei& "_.jpeg"
    File.MakeDir(File.DirRootExternal,"/data/data/a3a/cam/update/images")
    out = File.OpenOutput(File.DirRootExternal,"/data/data/a3a/cam/update/images/"&filename, False)
    out.WriteBytes(Data, 0, Data.Length)
    out.Close
    ToastMessageShow("Image Saved", True)
End Sub

Sub timer1_Tick
    camera1.TakePicture
    btnTakePicture.Enabled=False
End Sub
ching lee
  • 11
  • 7

1 Answers1

0

In your create tables code, you may need another column so that you can identify the image (id, name etc).

The code below will insert an image - taken from the basic4android website (http://www.b4x.com/android/forum/threads/sql-tutorial.6736/#content). You can almost always find the information you need in the forums there.

Sub InsertBlob
    'convert the image file to a bytes array
    Dim InputStream1 As InputStream
    InputStream1 = File.OpenInput(File.DirAssets, "smiley.gif")
    Dim OutputStream1 As OutputStream
    OutputStream1.InitializeToBytesArray(1000)
    File.Copy2(InputStream1, OutputStream1)
    Dim Buffer() As Byte 'declares an empty array
    Buffer = OutputStream1.ToBytesArray

    'write the image to the database
    SQL1.ExecNonQuery2("INSERT INTO table2 VALUES('smiley', ?)", Array As Object(Buffer))
End Sub
JohnL
  • 1
  • 1