Android Question Take Picture, Save to file as smaller pic, then save to DB

Harris

Expert
Licensed User
Longtime User
I have searched the threads, tried many possibilities, but still at loss.

I need to:

Take a picture with camera (using EZcamera - works great). Downsize this image since my Image View is very small (200 x 200 dip) and save it to file (I believe). Now take this image stored on disk and save it to my SQLite DB.

Seems simple enough but I stumble... Code example welcome.

Thanks
 

Harris

Expert
Licensed User
Longtime User
Ok, got it...

After camera captures new pic, I load it from disk, I scale it down (to about 122k - from 1.5 meg) and then write it back.

After, if client wishes to save it to the db (Sub ReplacePic), I read it into an Inputstream from disk and save the byte buffer to the blob.

Since photo (global) is in memory, can I save it to blob without the file.copy2 in/out method?

Thanks again.


B4X:
Sub Cam_PictureTaken (FileSaved As Boolean)
  If FileSaved = True Then
      If File.Exists(File.DirRootExternal, "TestPic.jpg") = True Then
   
        photo = LoadBitmap(File.DirRootExternal, "TestPic.jpg")
        photo =  DefCM.CreateScaledBitmap(photo, 480, 320, True)   
        'save image to file
        Dim out As OutputStream
        out = File.OpenOutput( File.DirRootExternal, "TestPic.jpg", False)
        photo.WriteToStream(out, 100, "JPEG")
        out.Close     
        Dim r As Rect
        r = cam.GetImageDimensions(File.DirRootExternal, "TestPic.jpg")
     
        taken = False
        ToastMessageShow("Picture Saved Size: "&r, True)
    End If
  End If
End Sub

Sub ReplacePic

    Dim OutputStream1 As OutputStream
    Dim InputStream1 As InputStream
    InputStream1 = File.OpenInput(File.DirRootExternal, "TestPic.jpg")
    OutputStream1.InitializeToBytesArray(1000)
    File.Copy2(InputStream1, OutputStream1)
   
    Dim Buffer() As Byte 'declares an empty array
    Buffer = OutputStream1.ToBytesArray
    'OutputStream1.Close
    DefCM.SQL1.ExecNonQuery2("Update InspType Set Image = ? where ID = ?",Array As Object(Buffer,recno))
    ShowImg
   
End Sub
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Yes. Instead of writing the photo to a file write it to an outputstream that was initialized with OutputStream.InitializeToBytesArray

Yes, works just fine. Thanks for the tip.
 
Upvote 0
Top