Android Question Save PNG file to SQL

DonManfred

Expert
Licensed User
Convert the image to bytes.
Convert the bytes to base64
Write the base64 string to db.

Read the base64 string from db
convert the base64 back to bytes
crate an image based on the bytes
Use the Image.

Some helpermethods
B4X:
Sub bytestobitmap(bytes() As Byte) As Bitmap
    Dim bmp As Bitmap
    Dim instr As InputStream
    instr.InitializeFromBytesArray(bytes,0,bytes.Length)
    bmp.Initialize2(instr)
    Return bmp
End Sub
Sub BytesToFile (Dir As String, FileName As String, Data() As Byte)
    Dim out As OutputStream = File.OpenOutput(Dir, FileName, False)
    out.WriteBytes(Data, 0, Data.Length)
    out.Close
End Sub
Public Sub ImageToBytes(Image As Bitmap) As Byte()
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    Image.WriteToStream(out, 100, "JPEG")
    out.Close
    Return out.ToBytesArray
End Sub

PS: It is not recommended to store images in a DB.
SQLite has a cursorlimit so you need to be careful on how many data you fetch from the db.

Best is to place the images on SDCard, only store the path and filename in the db and load the images from sdcard.
 
Last edited:
Top