Android Question How to save image and retrieve in DB

JhoeyBoy

Member
Licensed User
Longtime User
can anyone help me how to save a picture of a selected client from the gallery or selected photos from cellphone into my db file.
 

JhoeyBoy

Member
Licensed User
Longtime User
i used this code but it appears blank in the blob field



B4X:
Private Sub SwiftButtonBrowse_Click
    CC.Show("image/*", "Choose image")
End Sub


Sub CC_Result (Success As Boolean, Dir As String, FileName As String)
   
    If Success = True Then
        Log(FileName)
        ImageViewPhotoz.Bitmap = LoadBitmap(Dir,FileName)
        FileNamePhotoSel = FileName
        ImageViewPhotoz.Gravity = Gravity.FILL
        Dim bmp As Bitmap
        bmp.Initialize(Dir, FileName)
        Dim objOut As OutputStream
        objOut = File.OpenOutput(File.DirInternal,"lancer.png",True)
        bmp.WriteToStream(objOut,100,"PNG")
        objOut.Close
        Sleep(500)
        File.Copy(File.DirInternal,"lancer.png",File.DirDefaultExternal,"/data/" & "Jhoeyboy.png")
    Else
        FileName = "nophoto.jpg"
        ImageViewPhotoz.Bitmap = LoadBitmap(File.DirAssets,FileName)
        ToastMessageShow("): No Image Selected :(",True)
    End If
       
End Sub


save:
Msgbox2Async("ARE YOU SURE TO UPDATE PHOTO OF THIS CUSTOMER?","UPDATE PHOTO", "YES", "", "NO", LoadBitmap(File.DirAssets,"question.png"),False)
    Wait For Msgbox_Result (YesNo As Int)
    If YesNo = DialogResponse.NEGATIVE Then
        Return
    Else
   
        Dim InputStream1 As InputStream 'ignore
        InputStream1 = File.OpenInput(File.DirDefaultExternal,"/data/" & "Jhoeyboy.png")

        Dim OutputStream1 As OutputStream
        OutputStream1.InitializeToBytesArray(1000)
        Dim Buffer() As Byte
        Buffer = OutputStream1.ToBytesArray
       
        Log(Starter.SelectedCustomerID)
       
        Starter.SQL.ExecNonQuery2("UPDATE customer_profile SET Photo = ? WHERE CustIDNo = '" & Starter.SelectedCustomerID & "'", Array As Object(Buffer))
    End If
 
Upvote 0

JhoeyBoy

Member
Licensed User
Longtime User
this is my solution on how to save the image in the db file.
hope it will help to others with same problem..

Lancer



Browse Image in the Gallery:
Private Sub SwiftButtonBrowse_Click
    CC.Show("image/*", "Choose image")
End Sub


Sub CC_Result (Success As Boolean, Dir As String, FileName As String)
    
    If Success = True Then
        Log(FileName)
        ImageViewPhotoz.Bitmap = LoadBitmap(Dir,FileName)
        FileNamePhotoSel = FileName
        ImageViewPhotoz.Gravity = Gravity.FILL
        Dim bmp As Bitmap
        bmp.Initialize(Dir, FileName)
        Dim objOut As OutputStream
        
        Dim DateTimeVals As String
        DateTime.DateFormat = "hhmmssa"
        DateTimeVals = DateTime.Date(DateTime.Now)
        Log(DateTimeVals)
        ' Creat a unique filename of the selected image
        ImageBrowseFilename = Starter.SelectedCustomerID & "_" & DateTimeVals & ".png"
        Log(ImageBrowseFilename)
        
        objOut = File.OpenOutput(File.DirInternal,ImageBrowseFilename,True)
        bmp.WriteToStream(objOut,100,"PNG")
        objOut.Close
        Sleep(500)
        ' Copy the created image to your desired location
        File.Copy(File.DirInternal,ImageBrowseFilename,File.DirDefaultExternal,"/data/" & ImageBrowseFilename)
        UpdateIsOK = True
        
    Else
    ' if the user cancel or not select and image
        FileName = "nophoto.jpg"
        ImageViewPhotoz.Bitmap = LoadBitmap(File.DirAssets,FileName)
        ToastMessageShow("): No Image Selected :(",True)
    End If
        
End Sub
save image:
' Update Button

Private Sub SwiftButtonUpdateImage_Click
    ' if no image is selected
    
    If UpdateIsOK = False Then
        
        Dim cs As CSBuilder
        cs.Initialize.Color(Colors.White).Append("No Selected ")
        cs.Bold.Color(Colors.Green).Append("Photo for ").Pop.Pop
        cs.Append("this client! ").PopAll
        ToastMessageShow(cs, True)
        
        Return
    End If
    
    Msgbox2Async("ARE YOU SURE TO UPDATE PHOTO OF THIS CUSTOMER?","UPDATE PHOTO", "YES", "", "NO", LoadBitmap(File.DirAssets,"question.png"),False)
    Wait For Msgbox_Result (YesNo As Int)
    If YesNo = DialogResponse.NEGATIVE Then
        Return
    Else
        Log(ImageBrowseFilename)
        
        'convert the image file to a bytes array
        Dim InputStream1 As InputStream
        InputStream1 = File.OpenInput(File.DirDefaultExternal, "/data/" & ImageBrowseFilename)
    
        Dim OutputStream1 As OutputStream
        OutputStream1.InitializeToBytesArray(1000)   
        File.Copy2(InputStream1, OutputStream1)
    
        Dim Buffer() As Byte 'declares an empty array
        Buffer = OutputStream1.ToBytesArray
        
        Log(Starter.SelectedCustomerID)
        
        Starter.SQL.ExecNonQuery2("UPDATE customer_profile SET Photo = ? WHERE CustIDNo = '" & Starter.SelectedCustomerID & "'", Array As Object(Buffer))
    
        UpdateIsOK = False
        
        
    End If
    
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
hope it will help to others with same problem.
Small change you might want to implement is to use a fully parameterized query.
Instead of:
B4X:
 Starter.SQL.ExecNonQuery2("UPDATE customer_profile SET Photo = ? WHERE CustIDNo = '" & Starter.SelectedCustomerID & "'", Array As Object(Buffer))
Use this safer query:
B4X:
 Starter.SQL.ExecNonQuery2("UPDATE customer_profile SET Photo = ? WHERE CustIDNo = ?", Array As Object(Buffer,Starter.SelectedCustomerID))
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…