Android Question Not Displaying the Image from SQLite Database

Lyndon Bermoy

Active Member
Licensed User
I have made a sample on displaying the image file in the database and saved it as Blob data type. But the image file in the db doesnt show the image on my ImageView1.

Here's my code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("Layout1")
  If File.Exists(File.DirInternal,"dbImage.db") = False Then
     File.Copy(File.DirAssets,"dbImage.db",File.DirInternal,"dbImage.db")
   End If

   
   
   If sql1.IsInitialized = False Then
     sql1.Initialize(File.DirInternal, "dbImage.db", False)
   End If
   

  Dim Cursor1 As Cursor

  cursor1 = sql1.ExecQuery2("SELECT * FROM tblImage WHERE imgField = ?", Array As String("book3"))
  For i = 0 To cursor1.RowCount - 1
cursor1.Position = i
  Dim Buffer() As Byte 'declare an empty byte array
  Buffer = Cursor1.GetBlob("imgField")
  Dim InputStream1 As InputStream
  InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
   
  Dim Bitmap1 As Bitmap
  Bitmap1.Initialize2(InputStream1)
  InputStream1.Close
  ImageView1.SetBackgroundImage(Bitmap1)
   Next
End Sub
 

mangojack

Well-Known Member
Licensed User
It is a single image you are trying to display ..? What is the field name that contains "book3" ... ?
B4X:
Dim query as string
query = "SELECT imgField FROM tblImage WHERE ImageTitleField = ?" '@@@ Rplace ImageTitleField with field name containing "book3"
sql1.ExecQuery2(query, Array As String("book3"))
cursor1.Position = 0
Dim Buffer() As Byte      'declare an empty byte array
Buffer = Cursor1.GetBlob("imgField")
Dim InputStream1 AsInputStream
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
Dim Bitmap1 As Bitmap
Bitmap1.Initialize2(InputStream1)
InputStream1.Close
ImageView1.SetBackgroundImage(Bitmap1)
 
Last edited:

Lyndon Bermoy

Active Member
Licensed User
It is a single image you are trying to display ..? What is the field name that contains "book3" ... ?
B4X:
cursor1 = sql1.ExecQuery2("SELECT imgField FROM tblImage WHERE ImageTitleField = ?",ArrayAsString("book3")) '@@@ Rplace ImageTitleField with the proper field name containing "book3"
cursor1.Position = 0
Dim Buffer() As Byte      'declare an empty byte array
Buffer = Cursor1.GetBlob("imgField")
Dim InputStream1 AsInputStream
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
Dim Bitmap1 As Bitmap
Bitmap1.Initialize2(InputStream1)
InputStream1.Close
ImageView1.SetBackgroundImage(Bitmap1)

yes its a single image that I am trying to display. Book3 is a filename that i have uploaded on the blob data type of my database
 

mangojack

Well-Known Member
Licensed User
'have you tried the edited code ? .. Do you have a field that that identifies the image you wish to display .. ie "book3"

B4X:
Dim query As String
query = "SELECT imgField FROM tblImage WHERE !!ImageTitleField!! = ?"   '@@@ Rplace ImageTitleField with field name containing "book3"
sql1.ExecQuery2(query, Array As String("book3"))
cursor1.Position = 0
Dim Buffer() As Byte'declare an empty byte array
Buffer = Cursor1.GetBlob("imgField")
Dim InputStream1 As InputStream
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
Dim Bitmap1 As Bitmap
Bitmap1.Initialize2(InputStream1)
InputStream1.Close
ImageView1.SetBackgroundImage(Bitmap1)
 
Last edited:

mangojack

Well-Known Member
Licensed User
B4X:
SQL1.ExecNonQuery("CREATE TABLE tblImage (ID INTEGER PRIMARY KEY, imgTitle TEXT , imgField BLOB)")
 

Lyndon Bermoy

Active Member
Licensed User
'have you tried the edited code ? .. Do you have a field that that identifies the image you wish to display .. ie "book3"

B4X:
Dim query As String
query = "SELECT imgField FROM tblImage WHERE !!ImageTitleField!! = ?"   '@@@ Rplace ImageTitleField with field name containing "book3"
sql1.ExecQuery2(query, Array As String("book3"))
cursor1.Position = 0
Dim Buffer() As Byte'declare an empty byte array
Buffer = Cursor1.GetBlob("imgField")
Dim InputStream1 As InputStream
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
Dim Bitmap1 As Bitmap
Bitmap1.Initialize2(InputStream1)
InputStream1.Close
ImageView1.SetBackgroundImage(Bitmap1)

It gives me an error that Cursor1 is not initialized..
 

mangojack

Well-Known Member
Licensed User
sorry .. did not include cursor declaration ...

B4X:
Dim cursor1 AS Cursor
Dim query As String
query = "SELECT imgField FROM ..........................."
'......................................
 

edgar_ortiz

Active Member
Licensed User
You need to assign the result of the query to the cursor

Cursor1 = sql1.ExecQuery2(query, ArrayAsString("book3"))

Regards,

Edgar
 

mangojack

Well-Known Member
Licensed User
thanks @edgar_ortiz ...

B4X:
Dim cursor1 AS Cursor
Dim query As String
query = "SELECT imgField FROM tblImage WHERE !!ImageTitleField!! = ?"'@@@ Rplace ImageTitleField with field name containing "book3"
cursor1 = sql1.ExecQuery2(query, Array As String("book3"))
cursor1.Position = 0
Dim Buffer() As Byte'declare an empty byte array
Buffer = Cursor1.GetBlob("imgField")
Dim InputStream1 As InputStream
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
Dim Bitmap1 As Bitmap
Bitmap1.Initialize2(InputStream1)
InputStream1.Close
ImageView1.SetBackgroundImage(Bitmap1)
I thing I got it right this time ...:confused:
 

Lyndon Bermoy

Active Member
Licensed User
thanks @edgar_ortiz ...

B4X:
Dim cursor1 AS Cursor
Dim query As String
query = "SELECT imgField FROM tblImage WHERE !!ImageTitleField!! = ?"'@@@ Rplace ImageTitleField with field name containing "book3"
cursor1 = sql1.ExecQuery2(query, Array As String("book3"))
cursor1.Position = 0
Dim Buffer() As Byte'declare an empty byte array
Buffer = Cursor1.GetBlob("imgField")
Dim InputStream1 As InputStream
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
Dim Bitmap1 As Bitmap
Bitmap1.Initialize2(InputStream1)
InputStream1.Close
ImageView1.SetBackgroundImage(Bitmap1)
I thing I got it right this time ...:confused:

I got this error -_-
 

Attachments

Lyndon Bermoy

Active Member
Licensed User
The problem is: The Query do not return data... and the reason is that your table only have ONE field (imageField) and can not compare "imgField"

Regards

Edgar

P.S.:
In SqlLite is recomended that only use "lowercase" for the name of the tables and fields.
Thanks. I really need to study this :)
 
Top