B4J Question .jpg file in SQL DB store and Loader from SQL DB in ImageView

strupp01

Active Member
Licensed User
Longtime User
I try to save a company logo as a .jpg file in an SQL DB and then load this logo into an image viewer.
Property while looking for some examples of Android found, but nothing right for B4J.
For the store I wrote a code, but do not know if it is correct.
Who has an example or can check my code and supplement it?

B4X:
  Private Logo_Pfad, Logo_Name As String
   Dim FC As FileChooser
  
   FC.Initialize
   FC.Title = "Logo einfügen"
   FC.InitialDirectory = "C:\"
   '   Dim DirChosen As String

   FC.SetExtensionFilter("Image",Array As String("*.jpg","*.png","*.bmp"))
   Dim FileChosen As String
   FileChosen=FC.ShowOpen(frm)
  
   'convert the image file to a bytes array
   Dim InputStream1 As InputStream
   InputStream1 = File.OpenInput("", FileChosen)
   Dim OutputStream1 As OutputStream
   OutputStream1.InitializeToBytesArray(1000)
   File.Copy2(InputStream1, OutputStream1)
   Dim Buffer() As Byte 'declares an empty array
   Buffer = OutputStream1.ToBytesArray

   'write the image to the database
   SQL_DB.InitializeSQLite(File.DirApp,"Alle_Betriebsdaten.db", True)

   SQL_DB.ExecNonQuery("UPDATE Firma_Grunddaten SET Logo_Name = 'Logo', Logo_Image = '" & Array As Object(Buffer) & "'")
 
Last edited:

strupp01

Active Member
Licensed User
Longtime User
I do not understand yet.
How must the type for 'Logo_Image' be in the DB? BLOB?
How then must the command look like?
So? :
B4X:
SQL_DB.ExecNonQuery2("Update INTO Firma_Grunddaten SET Logo_Name = 'Logo', Logo_Image = ",Array As Object(FileChosen))

I would like to work with UPDATE
 
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
Ok, so I save the path with the file, but not the data itself. I thought you can save the data of the file so that I can load the data from the DB directly as ImageView again.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Ok, so I save the path with the file, but not the data itself. I thought you can save the data of the file so that I can load the data from the DB directly as ImageView again.
I'm going to guess that you used the code from this link (https://www.b4x.com/android/forum/threads/sql-tutorial.6736/#content) as your basis for storing the image in the database. The code published in that link uses the following to store the image to the database
B4X:
'write the image to the database
    SQL1.ExecNonQuery2("INSERT INTO table2 VALUES('smiley', ?)", Array As Object(Buffer))
In your initial code above, you used ExecNonQuery instead of ExecNonQuery2. So @Erel's initially replied and asked you to use ExecNonQuery2 instead of ExecNonQuery. The code sample that you then gave, did not store the picture, but the file name (look at it, you're using FileChosen). Based on that (that you are storing a file name), @Erel corrected your code. Assuming that you want to store the image file, not the file name, your code should be
B4X:
SQL_DB.ExecNonQuery2("UPDATE Firma_Grunddaten SET Logo_Name = 'Logo', Logo_Image = ?",Array As Object(Buffer))
Notes:
1) SQL UPDATE does not use INTO
2) As it stands, that update code would update every record in the table Firma_Grunddaten to have Logo_Name as 'Logo' and Every Logo_Image be the image you supply. I guess if there is only one record in Firma_Grunddaten, then that may be ok, but if it contains multiple records, you may not get the desired outcome.
 
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
Yes, the table has only one line.
Property now also the data stored in DB.
Problem is now the readout from the DB and represent in an ImageView.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
query the data. The content of the blobfield is a bytearray
Get the array and write it to an image on disc which you then can load into a imageview.
 
Upvote 0
Top