Android Question Saving Images Online/ to Database

Trinity Programming

Member
Licensed User
I am making an app where users can sign in to an account and also set their own profile picture. I have the usernames and passwords saved in an online database, but I don't know how to go about saving the profile pictures. I have heard saving images (or blobs) to databases is not advisory, so I've been trying to come up with an alternative means, but I am struggling. Any help with this and some sample code would be greatly appreciated!

Thanks!
 

Trinity Programming

Member
Licensed User
Since my images aren't too large, I am trying to save them on the database. However, I am still struggling to do this. This is my code so far.

B4X:
Sub Globals
    Dim bmp As Bitmap
    Dim out As OutputStream
    Dim inp As InputStream
End Sub


Sub Cam_PictureTaken (Data() As Byte)
   
    out = File.OpenOutput(File.DirRootExternal, teamNumber.Text & ".jpg", False)
    out.WriteBytes(Data, 0, Data.Length)
    out.Close
   
    inp.InitializeFromBytesArray(Data, 0, Data.Length)
    bmp.Initialize2(inp)
End Sub
I then save bmp to the database (in a column that is type: blob). Then this is my code to retrieve the picture. (It is inside a Select Case statement that gets the information back from the database)

B4X:
    Case "GetInfo"
                Dim ListOfInfo As List
                Dim TeamNumber As String
                Dim TeamName As String
                Dim Teampic As Bitmap
      
                ListOfInfo = parser.NextArray
               
                For i = 0 To ListOfInfo.Size - 1
                        Dim T As Map
                        T = ListOfInfo.Get(i)
                       
                        TeamNumber = T.Get("ScoutedTeam")
                       

                        If TeamNumber = ScoutedTeam Then
                               TeamName = T.Get("TeamName")
                               TeamNumber = T.Get("ScoutedTeam")
                               Teampic = T.Get("TeamPic")     'this is the code that should get the bitmap out of the database
                               InfoLabelNum.Text = TeamNumber
                              InfoLabelName.Text = TeamName
                              ImgViewTeamPic.SetBackgroundImage(Teampic)
                        End If
                       
                       
                    Next
When I run this code, I get this error message.

java.lang.RuntimeException: Object should first be initialized (Bitmap).

So, I'm assuming the bitmap Teampic needs to be initialized, but I don't know how I should initialize it. I tried doing this

B4X:
Teampic.Initialize2(inp)
But then I got this error message.

java.lang.RuntimeException: Object should first be initialized (InputStream).

So I am confused as to what I should I do.

Am I doing this the right way by just saving the bitmap into the database or should I be saving the input/output streams into the database?

Thanks for your help
 

DonManfred

Expert
Licensed User
EDIT: Oh, i didnt see that you are using JSON. My answer will not help then :-(

What is the type of the object when returning a blob? It is is an byte-array?
If yes you can try something like this

B4X:
Dim bmp As Bitmap
            Dim In1 As InputStream
          Dim buffer() As Byte          
            buffer = cur.Get("Picture")
            In1.InitializeFromBytesArray(buffer,0,buffer.Length)
            bmp.Initialize2(In1)          
            ImgViewTeamPic.SetBackgroundImage(bmp)
 
Top