Android Question {Closed] GetBytes problem

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All

I am looking to allow the user to export a HTML file from DirAssets to the SD Card.

In Erel's Texteditor example the following line converts the text to a byte array and is then streamed.

B4X:
Dim b() As Byte = txtField.Text.GetBytes("UTF8")

The following is what I have tried without success. [Plus many other variations and searches that failed.]

B4X:
Private HelpByte() As Byte = File.DirAssets&"/Helpfile.html.getbytes("UTF8")"


Could someone please enlighten me as to the correct format.

Regards Roger
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
String.GetBytes -> returns the bytes representing the string.
File.ReadBytes -> reads the file and return its raw content.

Practically:
B4X:
File.WriteString(s)
'same as
File.WriteBytes(s.GetBytes("utf8"))

Dim b() As Byte = s.GetBytes("utf8")
'same as
b = File.ReadBytes("file that we previously wrote")
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks again Erel,
Obvious when you know. GetBytes for Strings, ReadBytes for files. [EDIT: Maybe not so obvious.]

Unfortunately I still have basically the same problem but with ReadBytes. I must be formatting incorrectly but can't see the problem.
ReadBytes (Dir As String, FileName As String) As Byte()
Or???
B4X:
Dim b() As Byte
b = File.ReadBytes("file that we previously wrote")


B4X:
    Private Filename2 As String  = "helpfile.htm"
    Private HelpByte() As Byte = File.ReadBytes(File.DirAssets, Filename2)("UTF8")
    Private in As InputStream
    in.InitializeFromBytesArray(HelpByte, 0, HelpByte.Length)

This code Logs the following:

Main - 7072: Cannot cast type: {Type=String,Rank=0, RemoteObject=True} to: {Type=Byte,Rank=1, RemoteObject=True}
Main - 7070: Cannot cast type: {Type=Byte,Rank=0, RemoteObject=True} to: {Type=Byte,Rank=1, RemoteObject=True}
Main - 7072: Undeclared variable 'helpbyte'. (warning #8)

Sorry about this.

Regards Roger
 
Last edited:
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Where does this strange line come from? See the code I posted...

B4X:
Private HelpByte() As Byte = File.ReadBytes(File.DirAssets, Filename2)

Erel
The strange line comes from me trying to implement " ReadBytes (Dir As String, FileName As String) As Byte() " from B4A Files(Core), post#2.
I have seen the code you posted and I have no idea what it means or how it relates. The best I can guess is that it writes a string to a file and read bytes from the file. As I start with a file previously written file I assumed I only needed this line:

B4X:
b = File.ReadBytes("file that we previously wrote")

which resulted in the strange line.

Regards Roger
 
Upvote 0
Top