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:
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

Expert
Licensed User
Longtime 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:
Upvote 0

Lyndon Bermoy

Active Member
Licensed User
Longtime 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
 
Upvote 0

mangojack

Expert
Licensed User
Longtime 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:
Upvote 0

mangojack

Expert
Licensed User
Longtime User
B4X:
SQL1.ExecNonQuery("CREATE TABLE tblImage (ID INTEGER PRIMARY KEY, imgTitle TEXT , imgField BLOB)")
 
Upvote 0

Lyndon Bermoy

Active Member
Licensed User
Longtime 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..
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
sorry .. did not include cursor declaration ...

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

edgar_ortiz

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

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

Regards,

Edgar
 
Upvote 0

mangojack

Expert
Licensed User
Longtime 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:
 
Upvote 0

Lyndon Bermoy

Active Member
Licensed User
Longtime 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

  • imageError.jpg
    imageError.jpg
    103.6 KB · Views: 169
Upvote 0

mangojack

Expert
Licensed User
Longtime User
can you upload your project IDE > File > Export as Zip
 
Upvote 0

Lyndon Bermoy

Active Member
Licensed User
Longtime 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 :)
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
@edgar_ortiz is correct .. you have only single field in your db ..

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

Attachments

  • Sample Imagedb.zip
    47 KB · Views: 187
Upvote 0

edgar_ortiz

Active Member
Licensed User
Longtime User
Lyndon,

Try this... it's works... at least show your image...

Work a little bit with the logic of the process.

Regards

Edgar
 

Attachments

  • Test Db_Image.zip
    57.2 KB · Views: 193
Upvote 0
Top