Android Question insert photo from camera to database without saving to file

Cnrez

Member
Licensed User
Longtime User
hi, i want to know how exactly to do this...

i take a picture from camera, then i put it in imageview (another activity) by calling CallSubDelayed2 with
bitmap type output.
the picture displayed ok in that activity.
i'm using cameraEx class and ImageMod Code Module for maintaining proportion in imageview

but i want to save that picture to SQLlite database with Blob Type.

how do i do that ?

note : i have read this thread , but i do not want to save the image to file.

https://www.b4x.com/android/forum/threads/sql-tutorial.6736/#post39108

thank you.

here are piece of code that i'm using

activity Camera
B4X:
Sub Camera1_PictureTaken (Data() As Byte)
   camEx.StartPreview 'restart preview

    Dim in As InputStream
    in.InitializeFromBytesArray(Data, 0, Data.Length)
    Dim bmp As Bitmap
    bmp.Initialize2(in)
 
    CallSubDelayed2(ac_podbyda,"getphoto",bmp)
    Activity.Finish 
End Sub

activity ac_podbyda, displaying image and save to database
B4X:
Sub getphoto (byt As Bitmap)
    Dim bmp As Bitmap = byt
    Dim imgmodx As imgmod 
    imgmodx.Initialize
    imgvphoto.Bitmap = imgmodx.FitBitmapToView(bmp,imgvphoto) 
End Sub

B4X:
Dim ListOfMaps As List
ListOfMaps.Initialize
Dim inst As Map
inst.Initialize

inst.Put("image1", imgvphoto.Bitmap)

ListOfMaps.Add(inst)

DBUtils.InsertMaps(Starter.SQL1,"tt_photos",ListOfMaps)




 
Last edited:

Anser

Well-Known Member
Licensed User
Longtime User
I do not know what is
Dim imgmodx As imgmod
in your code. Searched the forum and did not find any Code module named imgmod for maintaining proportion in imageview

Please try the below given code. It is working fine for me.
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private oPhotoGraph() As Byte
End Sub
Sub Camera1_PictureTaken (Data() As Byte)
    oPhotoGraph = Data
    If Data = Null Then
        Msgbox("Unable to capture. Please try again","No picture captured")
    Else
        UploadPhoto
    End If
   
    camEx.StartPreview 'restart preview
End Sub

Sub UploadPhoto
    ProgressDialogShow("Saving Photo to database. Please wait....")
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name="insert_pic"
    cmd.Parameters=Array As Object(oPhotoGraph)

    reqManager.ExecuteCommand(cmd,"UploadPhoto")
End Sub

Regards
Anser
 
Upvote 0

Anser

Well-Known Member
Licensed User
Longtime User
but how do you implement it by using my code above ?
i am using dbutils, subcalldelayed2...
The only difference is that you are using DbUtils and I have used jRDC.

The data passed to the DB should be byte array, that's all

You can use CallSubDelayed2.
 
Upvote 0

Cnrez

Member
Licensed User
Longtime User
please anyone, help, i'm lost.

when i insert to database, the field contain :
[Ljava.lang.Object;@2669cb74

thank you for your help
 
Upvote 0

Cnrez

Member
Licensed User
Longtime User
ok thanks everyone
i solved it.

B4X:
Sub Globals
    Dim photo1() As Byte
End Sub

Sub getphoto (byt As Bitmap)
    Dim bmp As Bitmap = byt
    Dim imgmodx As imgmod 
    imgmodx.Initialize
    imgvphoto.Bitmap = imgmodx.FitBitmapToView(bmp, imgvphoto) 
End Sub

Sub getphotobyte (byt () As Byte)
    photo1 = byt 
End Sub

Sub btnsave_Click

Dim ListOfMaps As List
ListOfMaps.Initialize
Dim inst As Map
inst.Initialize

inst.Put("image1",photo1)

ListOfMaps.Add(inst)

DBUtils.InsertMaps(Starter.SQL1,"tt_photos",ListOfMaps)
End Sub

B4X:
Sub Camera1_PictureTaken (Data() As Byte)
   camEx.StartPreview 'restart preview

    Dim in As InputStream
    in.InitializeFromBytesArray(Data, 0, Data.Length)
    Dim bmp As Bitmap
    bmp.Initialize2(in)
 
    CallSubDelayed2(ac_podbyda, "getphoto", bmp)
    CallSubDelayed2(ac_podbyda, "getphotobyte", Data)
    Activity.Finish
End Sub

the problem lies in syntax to insert to database column.
wrong, not working :
inst.Put("image1",Array As Object(photo1))

correct :
inst.Put("image1",photo1)
 
Upvote 0
Top