Android Code Snippet [B4X] Bytes To File

Discussion in 'Code Snippets' started by Erel, Aug 17, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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 StringAs 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: Feb 11, 2018
  2. Erel

    Erel Administrator Staff Member Licensed User

    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 BitmapAs 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 Bitmap
       
    Dim In As InputStream
       
    In.InitializeFromBytesArray(bytes, 0, bytes.Length)
       
    Dim bmp As Bitmap
       bmp.Initialize2(
    In)
       
    Return bmp
    End Sub
    And XUI B4XBitmaps:
    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: Apr 4, 2018
  3. Cableguy

    Cableguy Expert Licensed User

    I just love rhetorics
     
    Rockefeller Goldman likes this.
  4. wonder

    wonder Expert Licensed User

    Is it possible to have a simple example of Bitmap --> Byte() and back, whitout using AsyncStreams?

    (should I start a new thread?)
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    AsyncStreams is not related. AsyncStreams takes care of sending the bytes.

    I've added the two code snippets to the second post.
     
  6. wonder

    wonder Expert Licensed User

    Thank you so much, Erel!!! :)
     
    Rockefeller Goldman likes this.
  7. dragonguy

    dragonguy Active Member Licensed User

    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
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    The code you posted will fail as B4XSerializator cannot serialize bitmaps directly. You should use ImageToBytes from the 2nd post instead.
     
    Rockefeller Goldman likes this.
  9. dragonguy

    dragonguy Active Member Licensed User

    OK thanks!
     
  10. sz4t4n

    sz4t4n Member Licensed User

    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: Aug 29, 2016
  11. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question and upload the base64 string as a text file.
     
  12. Toley

    Toley Active Member Licensed User

    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?
     
  13. sz4t4n

    sz4t4n Member Licensed User

    Just change bitmap to image
     
    DonManfred and Toley like this.
  14. cambopad

    cambopad Active Member Licensed User

    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 )?
     
  15. DonManfred

    DonManfred Expert Licensed User

    no
    Buy @Informatix ProBundle; you´ll get F5Steg which can hide a string in a picture. Not an image if i remember correctly.

    No, you can add a bytearray

    [​IMG]
     
    Last edited: Oct 5, 2016
    lemonisdead and cambopad like this.
  16. Jaames

    Jaames Active Member Licensed User

    There is a typo in the OP :
    :)
     
    Erel likes this.
  17. cambopad

    cambopad Active Member Licensed User

    Is there any other way that we can hide an image inside another image?
     
  18. DonManfred

    DonManfred Expert Licensed User

    i don´t know another way
     
    cambopad likes this.
  19. sorex

    sorex Expert Licensed User

    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?
     
    cambopad likes this.
  20. cambopad

    cambopad Active Member Licensed User

    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?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice