Android Question Imageview.bitmap 2 Blob and vice versa

Guenter Becker

Active Member
Licensed User
I looked through the posts (for example SQL Tutorial) and even find only statements pointing to transfer a bitmap file into a byte array (Dim Buffer() As Byte = File.ReadBytes(File.DirAssets, "smiley.gif")). But this is not I am looking for.

1st I need an b4x/b4a example how to transfer layout imageview.bitmap -> byte array -> byte array insert into sqlite database blob field and
2nd the way back from the blob field -> byte array -> imageview.bitmap.

Can anyone help me?
Thank you in advance Guenter
 

mangojack

Expert
Licensed User
Longtime User
from an old project ...

B4X:
'get ImageView bitmap and write to database
    Dim bmp As Bitmap = imvImage.Bitmap
    Dim OutputStream1 As OutputStream
    OutputStream1.InitializeToBytesArray(1000)
    bmp.WriteToStream(OutputStream1, 90, "JPEG")
    Dim Buffer() As Byte = OutputStream1.ToBytesArray
    oSQL.ExecNonQuery2("INSERT INTO tbl_Name VALUES (NULL, ?, ?)", Array As Object(title, Buffer ))


'get database image/blob and load to bitmap
    Dim Buffer() As Byte  = res.GetBlob("Image") 'res = ResultSet
    Dim InputStream1 As InputStream
    InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
    Dim Bmp As Bitmap
    Bmp.Initialize2(InputStream1)
    InputStream1.Close
    imvImage.Bitmap = Bmp
 
Last edited:
Upvote 0

Guenter Becker

Active Member
Licensed User
Great, big thank you this solution is working for me.
Stay well Guenter
 
Upvote 0

Guenter Becker

Active Member
Licensed User
from an old project ...

B4X:
get ImageView bitmap and write to database

    Dim bmp As Bitmap = imvImage.Bitmap

    Dim OutputStream1 As OutputStream

    OutputStream1.InitializeToBytesArray(1000)

    bmp.WriteToStream(OutputStream1, 90, "JPEG")

    Dim Buffer() As Byte = OutputStream1.ToBytesArray

    oSQL.ExecNonQuery2("INSERT INTO tbl_Name VALUES (NULL, ?, ?)", Array As Object(title, Buffer ))


'get database image/blob and load to bitmap

    Dim Buffer() As Byte  = res.GetBlob("Image") 'res = ResultSet

    Dim InputStream1 As InputStream

    InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)

    Dim Bmp As Bitmap

    Bmp.Initialize2(InputStream1)

    InputStream1.Close

    imvImage.Bitmap = Bmp
Hello I used the code and it still works but there are some questions left:

- in the code example a jpeg is used but what to do if we have a png with an alphachanel? I tried to convert a alphachanel png and it works but the alphachanel was lost and the transparent background was filled by black pixels. I assume the promlem is that the code above takes the png and converts it to a jpeg.

Does anyone know a solution?
 
Upvote 0

Guenter Becker

Active Member
Licensed User
Thank you is working, Happy New Year and stay well.
 
Upvote 0
Top