如何将摄像头直接存储到basic4android中的sqlite中

时间:2015-03-24 04:43:18

标签: basic4android

您好我想将所有摄像头捕获图像直接存储在basic4android中的sqlite数据库中。 我也创建了数据库和表。请告诉我如何将所有图像插入sqlite数据库? 请帮帮我。

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

1 个答案:

答案 0 :(得分:0)

在您的创建表代码中,您可能需要另一列,以便您可以识别图像(ID,名称等)。

下面的代码将插入一张图片 - 取自basic4android网站(http://www.b4x.com/android/forum/threads/sql-tutorial.6736/#content)。您几乎总能在论坛中找到所需的信息。

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