Android Question Imageview to stream

Tom Law

Active Member
Licensed User
Longtime User
I have an imageview (containing a customer signature). I would like to extract this image to a stream so that I can convert it to a byte array (there is no file, the image is in memory). There is probably a simple way to do this but I can't seem to work it out. Can someone tell me how to do this?
 

Tom Law

Active Member
Licensed User
Longtime User
Thanks Alexander, that's what I used to do but I was hoping to avoid writing to an image file. Let me explain, I want to extract the image to a stream then convert the data to base64 for storage in a database. (I was originally writing to file.dirrootexternal, but new permissions make this awkward. XUI.defaultfolder doesnt seem to work for me across different subs). How can I directly convert the image from the imageview without writing to a folder)?
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
B4X:
'Converts an image to a bytes array (for BLOB fields).
Public Sub ImageToBytes(Image As Bitmap) As Byte()
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    Image.WriteToStream(out, 100, "JPEG")
    out.Close
    Return out.ToBytesArray
End Sub
or
 
Upvote 0

Tom Law

Active Member
Licensed User
Longtime User
Thankyou very much Alexander, that has worked (and greatly simplified) my program. The only problem I have is that the bitmap background displays very darkly and it is difficult to read the signature. Is it possible to extract the signature onto a white background (as originally displayed on the imageview).
 
Upvote 0

Tom Law

Active Member
Licensed User
Longtime User
ALL SORTED thank you. By changing the JPEG part of the sub routine to PNG the bitmap now displays perfectly.
 
Upvote 0
Top