camera, SQL, blobs, and images

gfichter

New Member
Licensed User
Longtime User
I can't seem to save images to an sql lite database on my android tablet using the camera object. I am sure this is simple but I am having fits. I can take a picture and put it into memory. When I try to put it into a database and then retrieve it, I get nothing but errors.

this is my sample code assuming a table named images with only on column which is a blob:

Sub Camera1_PictureTaken (Data() As Byte)
camera1.StartPreview
Main.SQL1.ExecNonQuery("insert into Images values (" & Array As Object(Data) & ")")
ButtonTakePicture.Enabled = True
camera1.Release
End Sub
 

markh2011

Member
Licensed User
Longtime User
Hi,
This my code and it records the data into a sqlite table. I then use a button to run the sub.

Sub Camera1_PictureTaken (Data() As Byte)
camera1.StartPreview
Dim out As OutputStream
out = File.OpenOutput(File.DirDefaultExternal, a & ".jpg", False)
out.WriteBytes(data, 0, data.Length)
out.Close

Dim InputStream1 As InputStream
InputStream1 = File.OpenInput(File.DirDefaultExternal, a & ".jpg")
Dim OutputStream1 As OutputStream
OutputStream1.InitializeToBytesArray(1000)

File.Copy2(InputStream1, OutputStream1)
Dim Buffer() As Byte
Buffer = OutputStream1.ToBytesArray
'insert record
SQL1.ExecNonQuery2("INSERT INTO xxx VALUES(?, ?, ?, ?)", _
Array As Object(aa.Text, bb.Text, cc.Text, edtLocation.Text, ,(Buffer)))
End Sub

Sub btnSubmit_Click
camera1.TakePicture
End Sub
 
Upvote 0

COBRASoft

Active Member
Licensed User
Longtime User
I think you don't need an output and input stream, unless you really want to save the picture first. The data is already a byte array.
 
Upvote 0

gfichter

New Member
Licensed User
Longtime User
thanks markh and cobrasoft. Mark I will use your method until I can find a method by which I don't have to save it to the file system of the android unit. Your method has the picture saved twice, once to the filesystem and once to the database. I need it only saved to the database, but I really appreciate at least a working method as I am building a prototype.

Cobrasoft, I agree that the picture should already be in a byte array (data) and I should be able to save "data()" directly to the sql lite database. But for some reason that does not work, or I am not pulling it out of the database correctly.
 
Upvote 0

metrick

Active Member
Licensed User
Longtime User
? Any found a better way to save image to SQL blob fields after downloaded from url?
 
Upvote 0

吳界明

Member
Licensed User
Longtime User
Can you insert a picture into MySQL
but it can't work
why?
the photo field is a BLOB type , and Buffer is a byte data type
strSQL="INSERT INTO student(name,age,address,photo) VALUES (" & "'" & b & "'," & c & ",'" & d & "'," & Array As Object(Buffer) & ")"
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

吳界明

Member
Licensed User
Longtime User
Dim db As MYSQL
Dim Buffer() As Byte 'declares an empty array
Dim InStream As InputStream
InStream = File.OpenInput(File.DirRootExternal,"pic1.jpg")
Dim OutputStream1 As OutputStream
OutputStream1.InitializeToBytesArray(300000)
File.Copy2(InStream, OutputStream1)
Buffer = OutputStream1.ToBytesArray
OutputStream1.Close
db.setDatabase("xx.xxx.x.xx:3306","xxx","xxx","xxxxxxx")

Try

strSQL = "INSERT INTO student SET name = '" & b _
& "', age = " & c _
& ", address = '" & d _
& "', photo = " & Buffer _
& ";"
db.ExecuteNonQuery(strSQL)


I would like to insert the pic1.jpg into MySQL photo field, and photo is Long of BLOB data type,
but something is wrong of strSQL which has a message of "
src\Insert\water\gov\main.java:554: error: no suitable method found for NumberToString(byte[])
_strsql = "INSERT INTO student SET name = '"+_b+"', age = "+BA.NumberToString(_c)+", address = '"+_d+"', photo = "+BA.NumberToString(_buffer)+";";
^
method BA.NumberToString(Number) is not applicable
(actual argument byte[] cannot be converted to Number by method invocation conversion)
method BA.NumberToString(long) is not applicable
(actual argument byte[] cannot be converted to long by method invocation conversion) "
after compiled

Please give me the helps!
 
Upvote 0
Top