Android Question How to copy a bitmap into an SQLite BLOB

DickD

Active Member
Licensed User
I have a bitmap copied from the gallery into a bitmap variable bmp. I have tried using the sample code in the SQL tutorial shown below to put this in the SQL table. However this example only seems to work with image files and not bitmaps. The line that say File.Copy2 crashes if the first parameter is a bitmap. How can I do this? Can I convert the bitmap to an image file? What's the difference if any?

Private bmp as bitmap
....
bmp.Initialize(Dir, FileName)
...

Sub InsertBlob
Dim InputStream1 AsInputStream
' I already have the image in bmp so this next line is skipped
' InputStream1 = File.OpenInput(File.DirAssets, "smiley.gif")
Dim OutputStream1 AsOutputStream
OutputStream1.InitializeToBytesArray(1000)
File.Copy2(bmp, OutputStream1)
Dim Buffer() As Byte
Buffer = OutputStream1.ToBytesArray

SQL1.ExecNonQuery2("INSERT INTO table2 VALUES('smiley', ?)", ArrayAs Object(Buffer))
End Sub
 

DickD

Active Member
Licensed User
Please use [code]code here...[/code] tags when posting code.

ImageToBytes: https://www.b4x.com/android/forum/threads/b4x-bytes-to-file.70111/#content
Please excuse a beginner, at least with graphics files, but I don't really understand this and couldn't get it to work. What I think I need to do is convert the bitmap variable bmp. I cut and pasted the following from your example into my program. It compiled without any errors.

B4X:
Dim out As OutputStream
out.InitializeToBytesArray(0)
bmp.WriteToStream(out, 100, "JPEG")
out.Close

then I tried to insert 'out' into the SQL table with

B4X:
SQL1.ExecNonQuery2("UPDATE PersonalInfo SET image = ?) WHERE ID = 1", Array As Object(out.ToBytesArray))
or
SQL1.ExecNonQuery2("UPDATE PersonalInfo SET image = ?) WHERE ID = 1", Array As Object(out))

Both of these failed with a vague run time error. What next?
 
Upvote 0

samperizal

Active Member
Licensed User
Longtime User
try
B4X:
                 Dim out As OutputStream
                 out = File.OpenOutput(File.DirRootExternal, "PeopeWorksTemp.jpg",False)
                 out.WriteBytes(buffer, 0, buffer.Length)
                 out.Close
         Dim InputStream1 As InputStream
                 InputStream1 = File.OpenInput(File.DirRootExternal, "PeopeWorksTemp.jpg")
                 Dim OutputStream1 As OutputStream
                 OutputStream1.InitializeToBytesArray(1000)
                 File.Copy2(InputStream1, OutputStream1)
                 Dim buffer1() As Byte 'declares an empty array
                 buffer1 = OutputStream1.ToBytesArray

                 Main.sql_conexion.ExecNonQuery2("Update Clientes Set Imagen = ? where Codigo = ?",Array As Object(buffer1,Cliente_Codigo))
                 File.Delete(File.DirRootExternal, "PeopeWorksTemp.jpg")
 
Upvote 0

DickD

Active Member
Licensed User
try
B4X:
                 Dim out As OutputStream
                 out = File.OpenOutput(File.DirRootExternal, "PeopeWorksTemp.jpg",False)
                 out.WriteBytes(buffer, 0, buffer.Length)
                 out.Close
         Dim InputStream1 As InputStream
                 InputStream1 = File.OpenInput(File.DirRootExternal, "PeopeWorksTemp.jpg")
                 Dim OutputStream1 As OutputStream
                 OutputStream1.InitializeToBytesArray(1000)
                 File.Copy2(InputStream1, OutputStream1)
                 Dim buffer1() As Byte 'declares an empty array
                 buffer1 = OutputStream1.ToBytesArray

                 Main.sql_conexion.ExecNonQuery2("Update Clientes Set Imagen = ? where Codigo = ?",Array As Object(buffer1,Cliente_Codigo))
                 File.Delete(File.DirRootExternal, "PeopeWorksTemp.jpg")
try
B4X:
                 Dim out As OutputStream
                 out = File.OpenOutput(File.DirRootExternal, "PeopeWorksTemp.jpg",False)
                 out.WriteBytes(buffer, 0, buffer.Length)
                 out.Close
         Dim InputStream1 As InputStream
                 InputStream1 = File.OpenInput(File.DirRootExternal, "PeopeWorksTemp.jpg")
                 Dim OutputStream1 As OutputStream
                 OutputStream1.InitializeToBytesArray(1000)
                 File.Copy2(InputStream1, OutputStream1)
                 Dim buffer1() As Byte 'declares an empty array
                 buffer1 = OutputStream1.ToBytesArray

                 Main.sql_conexion.ExecNonQuery2("Update Clientes Set Imagen = ? where Codigo = ?",Array As Object(buffer1,Cliente_Codigo))
                 File.Delete(File.DirRootExternal, "PeopeWorksTemp.jpg")

Thanks, this worked. But now I am having a problem in the opposite direction. Now that I have an image in an SQL BLOB field I can't get it out to display it in a layout bitmap. I have tried the following
B4X:
Dim cursor1 As Cursor
Dim UI As InputStream
Dim bytes() As Byte
....
cursor1.Position = 0
bytes = cursor1.GetBlob("image")
UI.InitializeFromBytesArray(bytes, 0, bytes.Length)
bmp.Initialize2(UI)

All of this compiles successfully but then I get a run time error "error loading bitmap" on the last line.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Here you go:
B4X:
Dim Cursor1 As Cursor
Dim UI As InputStream
Dim bytes() As Byte
Cursor1 = SQL1.ExecQuery2("SELECT image FROM mytable WHERE country = ?", Array As String("PlanetB4A"))
Cursor1.Position = 0
bytes = Cursor1.GetBlob("image")  
UI.InitializeFromBytesArray(bytes, 0, bytes.Length)
Dim bmp As Bitmap
bmp.Initialize2(UI)
Activity.SetBackgroundImage(bmp)  'will show the image on the activity
 
Upvote 0

DickD

Active Member
Licensed User
Here you go:
B4X:
Dim Cursor1 As Cursor
Dim UI As InputStream
Dim bytes() As Byte
Cursor1 = SQL1.ExecQuery2("SELECT image FROM mytable WHERE country = ?", Array As String("PlanetB4A"))
Cursor1.Position = 0
bytes = Cursor1.GetBlob("image") 
UI.InitializeFromBytesArray(bytes, 0, bytes.Length)
Dim bmp As Bitmap
bmp.Initialize2(UI)
Activity.SetBackgroundImage(bmp)  'will show the image on the activity
This still produced the error "error loading bitmap" on the line "bmp.Initialize2(UI)"
 
Upvote 0
Top