Android Question Not Displaying the Image from SQLite Database

Lyndon Bermoy

Active Member
Licensed User
Longtime 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:
Expand Collapse Copy
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
 
It is a single image you are trying to display ..? What is the field name that contains "book3" ... ?
B4X:
Expand Collapse Copy
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:
Upvote 0
It is a single image you are trying to display ..? What is the field name that contains "book3" ... ?
B4X:
Expand Collapse Copy
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
 
Upvote 0
'have you tried the edited code ? .. Do you have a field that that identifies the image you wish to display .. ie "book3"

B4X:
Expand Collapse Copy
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:
Upvote 0
B4X:
Expand Collapse Copy
SQL1.ExecNonQuery("CREATE TABLE tblImage (ID INTEGER PRIMARY KEY, imgTitle TEXT , imgField BLOB)")
 
Upvote 0
'have you tried the edited code ? .. Do you have a field that that identifies the image you wish to display .. ie "book3"

B4X:
Expand Collapse Copy
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..
 
Upvote 0
sorry .. did not include cursor declaration ...

B4X:
Expand Collapse Copy
Dim cursor1 AS Cursor
Dim query As String
query = "SELECT imgField FROM ..........................."
'......................................
 
Upvote 0
You need to assign the result of the query to the cursor

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

Regards,

Edgar
 
Upvote 0
thanks @edgar_ortiz ...

B4X:
Expand Collapse Copy
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:
 
Upvote 0
thanks @edgar_ortiz ...

B4X:
Expand Collapse Copy
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

  • imageError.jpg
    imageError.jpg
    103.6 KB · Views: 398
Upvote 0
can you upload your project IDE > File > Export as Zip
 
Upvote 0
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 :)
 
Upvote 0
@edgar_ortiz is correct .. you have only single field in your db ..

study this example .. the db has 3 fields ..
 

Attachments

Upvote 0
Back
Top