Android Question (Solved) Image format for SQLite

Shelby

Well-Known Member
Licensed User
Longtime User
Soon, I'll be asking for help in having a value clickable to bring up an image in my SQLite db.
Using code from: https://www.b4x.com/guides/B4XSQLiteDatabase/?page=25 I see that the code has the following:
Insert an image::
Sub InsertBlob
    'convert the image file to a bytes array
    Private InputStream1 As InputStream
    InputStream1 = File.OpenInput(File.DirAssets, "smiley.gif")
    Private OutputStream1 As OutputStream
    OutputStream1.InitializeToBytesArray(1000)
    File.Copy2(InputStream1, OutputStream1)
    Private Buffer() As Byte 'declares an empty array
    Buffer = OutputStream1.ToBytesArray
    'write the image to the database
    SQL1.ExecNonQuery2("INSERT INTO table2 VALUES('smiley', ?)", Array As String(Buffer))
End Sub

I'm now creating a repository for my images. Shall I use the format of "GIF" for my images like in the code above, or would you suggest some other format like png, jpeg, Tiff, etc. I just noticed that my paint program makes a png image as default perhaps so that might be my number one choice.
Thanks,
 
Last edited:

Num3

Active Member
Licensed User
Longtime User
It depends on the images you are using.
The best compromise between size / quality is png, sometimes jpeg if you don't mind loosing a bit of quality.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
1) normally, you don't use the db to house a collection of images.
they are kept separately with a link in the db to them. a db with
images could be prohibitively too large for use on your device in
an app. there are other considerations (among which speed); you
should research them online for a better understanding.

2) the example doesn't "use" the .gif format. it
doesn't "use" any format; the image referenced happens
to be a .gif. the example simply copies the file byte by byte
into the db as a so-called "blob" type. (any kind of data can
be stored as a blob, but its handling depends on the
programmer's knowledge of what the blob represents.) the
example could have used a .jpg or .png. it merely showed
how you would store an image as a blob.

3) unless your collection is made up of smileys or animated
gifs, you don't want to convert your images to .gifs. you
will lose all color subtleties. (plus you'll need support for the
gif format in android.) the same holds for other formats
such as .tifs. (the .tif format is lossless,which means the files
are often very large and contain information relevant for file
editing not display, plus you'll have to have a way of displaying
them (you'll need a separate viewer app...).

4) if your image collection is already in the .jpg format, you don't
want to convert it to something else. .jpgs are "lossy"; every
time you open one to edit or convert it, you lose even more
detail. if you're looking to use a "better" format going forward, you
could consider .png.
 
Upvote 0

Shelby

Well-Known Member
Licensed User
Longtime User
Thanks, Doctor,
Since my paint program is simplest using png, I think I'll choose it. I realize that the images will be housed separately from the db with a link to them. I'll continue building my repository of images.
 
Upvote 0

emexes

Expert
Licensed User
I'm now creating a repository for my images. Shall I use the format of "GIF" for my images like in the code above, or would you suggest some other format like png, jpeg, Tiff, etc.
JPEG for photos, PNG for everything else, especially plans and diagrams.

WEBP is great but still a bit new.
 
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
I use base64.
+1

Use this library to encode images to Base64 then save the string into your database. You can use the same library to decode the Base64 image string from the database back into an image to place into an imageview for example. I've encoded/decoded images into and out of all types of databases for 20+ years using this method with no issues whatsoever.

Use this library



Enjoy...
 
Last edited:
Upvote 0

Shelby

Well-Known Member
Licensed User
Longtime User
Hmm,
I do want to encrypt the entire app soon so does that help? I mean the Base 64 and library.
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
Hmm,
I do want to encrypt the entire app soon so does that help? I mean the Base 64 and library.
Encryption is not related to base64 conversion of image.

image is binary data -> base64 conversion -> ascii data (alphanumeric characters only)
so after conversion your image becomes a string of text, which you can easily store in db like name, address etc.
 
Upvote 0
Top