Android Question Best practice in storing and retrieving and image to and from MySQL database

Gandy5

Member
Greetings,

I'm new to app development and I need your help. I'm developing an app, not a web app but and Android mobile app that will store user details including their pictures.

I earlier tried storing the images in a file server and the image path in my MySQL database. But when retrieving it, to place in in the imageview, it is very slow since it will have to first download the image from the file server.

I have not tried the base64 option since I don't know how to pass it as a parameter to my php script and I don't know whether it will solve my problem i.e. the slowness in retrieving the image.

Thank you.
 
Last edited:

MicroDrie

Well-Known Member
Licensed User
Hi Gandy5, Speed is a relative term. The higher the resolution and the larger the image, the more information has to be moved, which need time. As a result, the transport takes longer. A point of attention is that there comes a time when the Base64 string length may exceed the maximum allowed length, which lead that information may be lost. The conversion to Base64 string produces even more bytes that have to be transferred. Storing a Base64 string against it is nothing but a long string of text characters. Seems to me the simplest one for PHP. There is a good Base64EncodeDecodeImage library for B4A and B4J
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
as suggested by microdrie, a base64 string is larger than the original image, so even slower to load. store and/or transfer a smaller image. you said the app stores user details and their picture. once the image is on the user's device, there will be no need to transfer it again. and even if the original image comes from a selfie, there would only be a single transfer (presumably to the server for safe-keeping). you'd have to say "processing..." 1 time; how terrible is that?
 
Upvote 0

Xicu

Active Member
Licensed User
Longtime User
In my opinion this its the best way
storing the images in a file server and the image path in my MySQL database
, because (read this)

You said
But when retrieving it, to place in in the imageview, it is very slow
Why don't you first download a reduced format image from the server? Using php I think that you can reduce the image as a preview (I do that but using a webservice with asp.net .asmx). Put the reduced image downloaded into a imageview. And if the user really wants to download it, then allow him to do.

The current mobiles allow to obtain photos with many megabytes.
In my case, I upload the photos to a company fisical server. So, before uploading the image, I check its size. If it is excessive, I reduce it to a more "normal" size.
This way I control the use of server disk space
 
Last edited:
Upvote 0

Xicu

Active Member
Licensed User
Longtime User
You can control the size of your photos by "playing" with their dimensions, quality or both

1-You can determine that the photos you upload are no larger than a limit
B4X:
Sub ResizeImage (Dir As String, FileName As String, MaxWidthHeigth As Int) As Bitmap
    'DETERMINE IF WE HAVE TO REDUCE THE PHOTO AND WHAT DIMENSIONS
    Dim MaxSize As Int = MaxWidthHeigth
    Dim NewWidth As Int
    Dim NewHeight As Int
    Dim b As Bitmap
    b.Initialize(Dir,FileName)
    If     b.Width>b.Height Then
        'Horitzontal
        'We review if we have to reduce, and if so, what new measure
        If b.Width> MaxSize Then
            NewWidth=MaxSize
            NewHeight=b.Height * MaxSize/b.Width
        Else
            NewWidth=b.Width
            NewHeight=b.Height
        End If
    Else
        'vertical
        If b.Height>MaxSize Then
            NewHeight=MaxSize
            NewWidth=b.Width * MaxSize/b.Height
        Else
            NewWidth=b.Width
            NewHeight=b.Height
        End If
    End If
    Return b.Resize(NewWidth, NewHeight, True)
End Sub

For example, if I want to set that all photos have a maximum measurement of 1600 pixels (long or high), I do that...
B4X:
'Resize image
Dim tempbitmap As Bitmap
tempbitmap =ReziseImage(Dir,FileName, 1600)'<<<<<<<<<<<CALL RESIZEIMAGE SUB WITH THE MAX DIMENSION THAT I WANT
'Save reduced image
Dim out As OutputStream
out = File.OpenOutput(File.DirInternal, FileName ,  False)
tempbitmap.writeToStream(out, 100, "JPEG")
out.Close
'Call sub to upload photo with the rest of information (date, name, etc..)

2-You can reduce the size modifyin the photo quality. (In this case you dont call sub ResizeImage)
B4X:
'Resize image
Dim tempbitmap As Bitmap
'tempbitmap =ReziseImage(Dir,FileName,1600)<<<<<<<<<<<<COMENT THIS LINE
tempbitmap=LoadBitmap(Dir,FileName)<<<<<<<<<<<<<<<<<ADD THIS LINE
'Save reduced image
Dim out As OutputStream
out = File.OpenOutput(File.DirInternal, FileName ,  False)
tempbitmap.writeToStream(out, 70, "JPEG")<<<<<<<<<<<<<<<MODIFY THE QUALITY VALUE (70 I TESTED THAT ITS OK)
out.Close
'Call sub to upload photo with the rest of information (date, name, etc..)
3-Combine size and quality, calling reziseImage sub, and modify quality value in tempbitmap.writeToStream(out, 70, "JPEG")
 
Last edited:
Upvote 0
Top