Android Code Snippet [B4X] Bytes To File

This code is no longer needed. Use File.WriteBytes / ReadBytes.

Write an array of bytes to a file and read a file into an array of bytes.

Code:
Sub BytesToFile (Dir As String, FileName As String, Data() As Byte)
   Dim out As OutputStream = File.OpenOutput(Dir, FileName, False)
   out.WriteBytes(Data, 0, Data.Length)
   out.Close
End Sub

Sub FileToBytes (Dir As String, FileName As String) As Byte()
   Return Bit.InputStreamToBytes(File.OpenInput(Dir, FileName))
End Sub

Note that you can convert complex objects (and simple objects) to bytes and vice versa with B4XSerializator from the RandomAccessFile library

Tags: Bytes, Serialization

Working with bytes? Check BytesBuilder: https://www.b4x.com/android/forum/t...implifies-working-with-arrays-of-bytes.89008/
 
Last edited:
And what about strings?

Code:
Dim s As String = "abcde"
Dim bytes() As Byte = s.GetBytes("UTF8")
'and back
s = BytesToString(bytes, 0, bytes.Length, "UTF8")

And bitmaps?

Code:
Public Sub ImageToBytes(Image As B4XBitmap) As Byte()
   Dim out As OutputStream
   out.InitializeToBytesArray(0)
   Image.WriteToStream(out, 100, "JPEG")
   out.Close
   Return out.ToBytesArray
End Sub

Public Sub BytesToImage(bytes() As Byte) As B4XBitmap
   Dim In As InputStream
   In.InitializeFromBytesArray(bytes, 0, bytes.Length)
#if B4A or B4i
   Dim bmp As Bitmap
   bmp.Initialize2(In)
#else
   Dim bmp As Image
   bmp.Initialize2(In)
#end if
   Return bmp
End Sub
 
Last edited:
I just love rhetorics
 
Is it possible to have a simple example of Bitmap --> Byte() and back, whitout using AsyncStreams?

(should I start a new thread?)
 
i usually use below code to convert object to byte

Code:
Dim bmp As Bitmap
    bmp.Initialize(File.DirDefaultExternal,"test.jpg")
    Dim data() As Byte=object_to_byte(bmp)
    AStreams.Write(data)

Code:
Sub object_to_byte(obj As Object)As Byte()
    Dim ser As B4XSerializator
    Return ser.ConvertObjectToBytes(obj)
End Sub

Sub byte_to_object(data() As Byte)As Object
    Dim ser As B4XSerializator
    Return ser.ConvertBytesToObject(data)
End Sub
 
Im trying to use Your code snippets to send image from android to my PC ( B4j ) via MQTT. I'm sending bytes from android and i receive it correctly.

This is what i receive "


im trying to convert it to an image using this code :

Code:
ImageView1.SetImage(BytesToImage(Payload))

Public Sub BytesToImage(bytes() As Byte) As Image
  
      Dim in As InputStream
      in.InitializeFromBytesArray(bytes,0,bytes.Length)
      Dim bmp As Image
       bmp.Initialize2(in)
      Return bmp
 
End Sub

and nothing happens. Imageviewer doesnt change. Any advise?
 
Last edited by a moderator:
Hi Erel, the bitmap example do not compile in B4J because Bitmap is not recognized in B4J.
What is the best way to do the same in B4J?
 
Public Sub ImageToBytes(ImageAsBitmap) As Byte()Dim out AsOutputStream
out.InitializeToBytesArray(0)Image.WriteToStream(out, 100, "JPEG")
out.CloseReturn out.ToBytesArrayEnd Sub

Can we use this sub to add image A into Image B to hide our original image?

I meant can we hide one image in another image (steganography )?
 
Last edited:
Is there any other way that we can hide an image inside another image?

yes, you can do some trickery with jpg files to add extra data but you'll need to write your own tools for it to create and extract it.

question is why would you do it?
 
yes, you can do some trickery with jpg files to add extra data but you'll need to write your own tools for it to create and extract it.

question is why would you do it?

I am planing to create an app that allows the user to hide their photo on their phone. The best solution is to hide their photo inside another photo. Doing so will help make the photo to be hidden out of the attention of the stealer.


If you know how to do it, can you show me some example code?
 
Back
Top