Sub Activity_Create(FirstTime As Boolean)
Dim su As StringUtils
Dim b() As Byte
b = su.DecodeBase64(File.ReadString(File.DirAssets, "1.txt"))
Dim in As InputStream
in.InitializeFromBytesArray(b, 0, b.Length)
Dim out As OutputStream
out = File.OpenOutput(File.DirRootExternal, "1.jpg", False)
File.Copy2(in, out)
out.Close
Log(b.Length)
Dim bmp As Bitmap
bmp.Initialize2(in)
Activity.SetBackgroundImage(bmp)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
trasmiUdpHost.Connect(GLOIP, GLOINTPORT)
'Dim strReturnData As String
Dim path As String = "C:\Users\florew1\Pictures\purple5.jpg"
Dim sr As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read)
Dim bytes As [Byte]() = New Byte((sr.Length) - 1) {}
Dim sw As IO.StreamWriter
sw = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\florew1\Desktop\testfile.txt", True)
Dim numBytesToRead As Integer = CType(sr.Length, Integer)
Dim numBytesRead As Integer = 0
'Dim sendata As String
'Dim sendata1 As String
Dim sendata2() As Byte
While (numBytesToRead > 0)
' Read may return anything from 0 to numBytesToRead.
Dim n As Integer = sr.Read(bytes, numBytesRead, numBytesToRead)
' Break when the end of the file is reached.
'strReturnData = System.Text.Encoding.Unicode.GetString(bytes)
sr.Close()
Dim converted As String
' sendata = System.Text.Encoding.Unicode.GetString(bytes)
'sendata1 = EncodeBase64(sendata)
converted = ConvertFileToBase64(path)
sendata2 = Encoding.ASCII.GetBytes(converted)
'ExtractBase64ToFile(converted, "image.gif") 'if i uncomment this line the the file gets reconstructed and i can see the image
pRet = trasmiUdpHost.Send(sendata2, sendata2.Length)
sw.WriteLine(converted)
sw.Close()
If (n = 0) Then
Exit While
End If
numBytesRead = (numBytesRead + n)
numBytesToRead = (numBytesToRead - n)
End While
numBytesToRead = bytes.Length
' For i = 0 To bytes.Length - 1
' pRet = trasmiUdpHost.Send(bytes, bytes.Length)
End Sub
Public Function ConvertFileToBase64(ByVal fileName As String) As String
Dim ReturnValue As String = ""
If My.Computer.FileSystem.FileExists(fileName) Then
Using BinaryFile As FileStream = New FileStream(fileName, FileMode.Open)
Dim BinRead As BinaryReader = New BinaryReader(BinaryFile)
Dim BinBytes As Byte() = BinRead.ReadBytes(CInt(BinaryFile.Length))
ReturnValue = Convert.ToBase64String(BinBytes)
BinaryFile.Close()
End Using
End If
Return ReturnValue
End Function
Use a StreamWriter instead and explicitly set the encoder (c# code):
B4X:StreamWriter sw = new StreamWriter(path, false, new UTF8Encoding(false))
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?