Writing image to byte array

XverhelstX

Well-Known Member
Licensed User
Longtime User
Hey everyone,

I pick a file with filedialog and I want to put it into a bytearray.
I think this should do it, but nothing is written into it.

B4X:
Dim fd As FileDialog
Dim outlayImage As OutputStream
Dim overlayImage() As Byte

btmpImageLoader.Initialize(File.DirRootExternal, fd.ChosenName)
outlayImage = File.OpenOutput(File.DirRootExternal,fd.ChosenName,False)
btmpImageLoader.WriteToStream(outlayImage,100,"JPEG")
outlayimage.InitializeToBytesArray(0)
overlayImage = outlayimage.ToBytesArray

B4X:
Msgbox(overlayImage.Length,"")
Dim msg As String
msg = BC.HexFromBytes(overlayImage)
Msgbox(msg,"")

Lenght gives 0 and msg gives nothing
2) Also, I have 2 files in my external SD root, but I cannot seem to open livestream.jpg but I can open .bmp
B4X:
btmpImageLoader.Initialize(File.DirRootExternal, "livestream.bmp")
but not
B4X:
btmpImageLoader.Initialize(File.DirRootExternal, "livestream.jpg")
Error loading bitmap.

In the documentation, a jpg is used:
B4X:
Initialize (Dir As String, FileName As String)

Initializes the bitmap from the given file.
Example:
Dim Bitmap1 As Bitmap
Bitmap1.Initialize(File.DirAssets, "X.jpg")

Strangen here I just found out that if I remove livestream.jpg from my sd card, they place it again on my root. Open the app, select the file, it works. Second time I try (without removing) I got an error loading bitmap.
 
Last edited:

gemasoft

Member
Licensed User
Longtime User
Bytes to image

Good to all is my question like to return to generate the image from an arrangement of Byte? (Ej:image -> bytes -> image)
 
Upvote 0

gemasoft

Member
Licensed User
Longtime User
Thank you very much for the reply, but my problem now is that this function loads the image I .. I try to do is take a picture with a device to send and put that picture as wallpaper in a device B. Not sure if it really works like this implementation.

// device B
Sub AStreams_NewData (Buffer() As Byte)

Dim in As InputStream
in.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
Dim bmp As Bitmap
bmp.Initialize2(in)
in.Close

Dim bmp_dra As BitmapDrawable
bmp_dra.Initialize(bmp)

Activity.Background=bmp_dra
End Sub
 
Upvote 0

gemasoft

Member
Licensed User
Longtime User
Erel thank you very much, was whether it was booting this way
' ' AStream.Initialize'(..)
AStream.InitializePrefix (..)
now it works.
 
Upvote 0

PFlores81

Active Member
Licensed User
Longtime User
Erel


I've got this setup for encryption however, how would I pull said file and decrypt back to its original location?
 
Upvote 0

Kwame Twum

Active Member
Licensed User
Longtime User
Loading a file into a byte array can be done with this code:
B4X:
    Dim in As InputStream
    in = File.OpenInput(...)
    Dim out As OutputStream
    out.InitializeToBytesArray(1000)
    File.Copy2(in, out) '<---- This does the actual copying
    Dim data() As Byte
    data = out.ToBytesArray

Hello, please I tried using this to convert an image (.jpg) into byte array and got the following:
error: incompatible types: byte[] cannot be converted to byte
Need help please.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Top