Android Question blob on Imageview? - Solved

anOparator

Active Member
Licensed User
Longtime User
I used SQLite to make database = test2,
table = stuff:
There are 3 records for [_id] Numeric, [name] Text, [image] Blob

Am wanting to understand how to alternate image1, 2, and 3 on an Imageview (just noticed there is none in the SQL Blob tutorial) using:

B4X:
   Cursor1 = SQL1.ExecQuery2("SELECT image FROM stuff WHERE name = ?", Array As String("name"))

             OR
  Buffer = Cursor1.GetBlob2(0)  'ERROR: CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0


After 2 weeks and a bunch of errors I still need help with the syntax.
All tips appreciated.

B4A v4.3
.NET Framework 4.5
 

Attachments

  • sqlBlob.zip
    74.5 KB · Views: 198

Mahares

Expert
Licensed User
Longtime User
This should accomplish it for you:
B4X:
Dim imageview1 As ImageView
    Cursor1 = SQL1.ExecQuery2("SELECT image FROM stuff WHERE name = ?", Array As String("name"))
    Cursor1.Position = 0
    Dim Buffer() As Byte 
    Buffer = Cursor1.GetBlob("image")
    Dim InputStream1 As InputStream
    InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
    Dim Bitmap1 As Bitmap
    Bitmap1.Initialize2(InputStream1)
    InputStream1.Close
    imageview1.SetBackgroundImage(Bitmap1)
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
Thanks for the pointers, compiles better. I put the underlined text in and get my msgbox, will re-create test2.db.

Cursor1 = SQL1.ExecQuery2("SELECT image FROM stuff WHERE name = ?", Array As String("name"))
Cursor1.Position = 0
If Cursor1.RowCount = 0 Then ' Initialize Cursor error.
Msgbox("any Rows", "questiion")
End If

Dim Buffer() As Byte
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User

Hello again, please tell me if these Debug pics are telling me that Cursor1 and Imageview1 are not initialized, or is the problem in the Sub?
B4X:
Sub ReadBlob
   Private ImageView1 As ImageView
   Cursor1 = SQL1.ExecQuery2("SELECT image FROM stuff WHERE name = ?", Array As String("name"))
    Cursor1.Position = 0
   Dim Buffer() As Byte
  Buffer = Cursor1.GetBlob("image")    ' Error = CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
  Dim InputStream1 As InputStream
  InputStream1.InitializeFromBytesArray(Buffer, 2, Buffer.Length)
  Dim Bitmap1 As Bitmap
  Bitmap1.Initialize2(InputStream1)
  InputStream1.Close
  ImageView1.SetBackgroundImage(Bitmap1)
    ImageView1.Color=Colors.Cyan       ' Color does NOT appear
  
'    If Cursor1.RowCount < 5 Then     ' Msgbox does NOT appear with 3 images in the db.
    If Cursor1.RowCount > 0 Then       ' Msgbox does NOT appear with 3 images in the db.
   Msgbox("any Rows", "what")
End If
End Sub
thanks in advance

B4A v4.3
.NET Framework 4.5
Java x86
C:\Android
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Cursor1 = SQL1.ExecQuery2("SELECT image FROM stuff WHERE name = ?", Array As String("name"))
will result in something like
SELECT image FROM stuff WHERE name = "name"
Does this find a record in your table?
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
will result in something like

Does this find a record in your table?
Thanks but Cursor1 = SQL1.ExecQuery2("SELECT image FROM stuff WHERE name = name", Array As String("name")) gives error:
android.database.sqlite.SQLiteBindOrColumnIndexOutOfRangeException: bind or column index out of range: handle 0x1625e88
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
will result in something like

Does this find a record in your table?
Thanks so much for the pointer. SQL1.ExecQuery without extra argument enabled sending db image using Button1_Click to Imageview.

Will update post in a few days
 
Upvote 0

derez

Expert
Licensed User
Longtime User
This
Cursor1 = SQL1.ExecQuery2("SELECT image FROM stuff WHERE name = name", Array As String("name"))
should be:
B4X:
Cursor1 = SQL1.ExecQuery2("SELECT image FROM stuff WHERE name = ?", Array As String("name"))
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
This
should be:
B4X:
Cursor1 = SQL1.ExecQuery2("SELECT image FROM stuff WHERE name = ?", Array As String("name"))
Thanks I got it working with,
B4X:
Cursor1 = SQL1.ExecQuery("SELECT image FROM stuff WHERE name = name").
Didn't need the 'Array As String("name"))' argument because that is the Value of the smiley_face.jpg in the sample.

Now I have
B4X:
Cursor2 = SQL1.ExecQuery("SELECT name FROM stuff WHERE name = name").
in 'Sub ReadBlob' to place the records text on Label1 at the same time the image goes on Imageview1.

And lastly, Button2_Click fires 'Sub ReadBlob' and does Cursor1.Position=Cursor1.Position + 1 to show the next recordset, etc.
Maybe tomorrow Button1 gets replaced by a timer.
Kudos for the reading material everyone.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…