Android Question How to storage image natively and load it when needed?

aironium

Member
(I know it's a bit repetitive, but many of the entries in the forum are confusing to me, I'm sorry)

Continuing with my capstone project, we had to store an image for the customer profile, and it seems that using Base64 encoding is not an efficient approach.

I am curious what is the basic principles of storing images to the file storage (or just in the app storage at least) and load it when needed (e.g. when viewing customer profile).

(Optionally, how can I also upload it to a remote server as well?)
 

Knoppi

Active Member
Licensed User
Longtime User
I'm not sure if I understand you correctly.
you can load/save pictures like this
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim myBmp As Bitmap = LoadBitmap( File.DirInternal, "user_1.jpg")
    SaveBitmap( myBmp, File.DirInternal, "user_2.jpg")
End Sub

Sub SaveBitmap( bmp As Bitmap, Dir As String, Filename As String)
    Dim Out As OutputStream
    Out = File.OpenOutput( Dir, Filename, False)
    bmp.WriteToStream( Out, 100, "JPEG")
    Out.Close
End Sub
 
Upvote 0

emexes

Expert
Licensed User
and it seems that using Base64 encoding is not an efficient approach.

Firstly, if it's a photograph then make sure you are storing it compressed, not as uncompressed 3-bytes-per-pixel. JPEG is the usual and common format.

Assuming you're using a SQL database somewhere, you can either store the JPEG image in the database (assuming it fits into a Binary Large OBject aka BLOB), or save them as JPEG files using say customer id as filename eg 7738-PROFILE.JPG or PR7738.JPG and then just save the filename in the database, or even just regenerate the filename on the fly if you're using id that's already in the database (eg customer id 7738 = profile image filename 7738-PROFILE.JPG).

Bear in mind you might have multiple images for a customer, so it'd be nice to have a naming scheme that lets you get a list (DIR) of all image files for a given customer id.

You don't need to have a dash/hyphen in the filename, but it's usually A Good Idea if you are using variable-length id, so that when you search for files belonging to customer 773 you don't also get files belonging to customers 7731, 7732, 773456, etc.

Btw probably best to NOT use names as id, because names can change (by eg typo correction or marriage or if the customer is a company).
 
Upvote 0

emexes

Expert
Licensed User
Also, if your profile pictures are hot-of-the-press from your XX-megapixel camera, you might want to resize them down to something more reasonable, especially if it's just used as a profile picture and unlikely to ever be shown more than say 640x640 pixels (= 1-inch square at 640 ppi).
 
Upvote 0
Top