Reading binary data from File.DirAssets - solution wanted

basil99

Active Member
Licensed User
Have some info, stored in a binary file ( like game levels, animation logic etc, never mind what ).

File is located in File.DirAssets, so it's a part of a package.

First, I was happy to know RandomAccessFile lib exist, but when i read manual, I realise it can't read files from APK, because its actually a ZIP file.

So, what the correct solution for that ? As I got, no way, but copy files from File.DirAssets to a place, where RandomAccessFile can work, like this sample by Erel - http://www.basic4ppc.com/forum/basic4android-updates-questions/17698-one-time-copy-files-assets-defaultexternal.html#post101512

Is this correct ? Or another solution exist ?

Thank you
 

basil99

Active Member
Licensed User
One solution is to copy the file to any other folder. The second solution is to read it to memory (if it is not too large) and then access the bytes array.
This exactly what i need. Any sample code/lib for reading binary into mem ?
I need smth like this ( pseudocode ):

dim Array() as int
memoryadress = ReadFile( file as string )

for e=0 to Length

Array( e ) = Peek( memoryadress + e )

next e


Thank you
 

basil99

Active Member
Licensed User
B4X:
Sub ReadFile(Dir As String, FileName As String) As Byte()
   Dim out As OutputStream
   out.InitializeToBytesArray(100) 'size not really important
   File.Copy2(File.OpenInput(Dir, FileName), out)
   Return out.ToBytesArray
End Sub
You should then use RAF.Initialize3.
Thanks, but stuck again. Code above reads file into Bytes Array like this:

Dim MyArray() As Byte()
MyArray() = ReadFile( Dir, filename )

why to use RAF ? File is already in memory as array of bytes ?
 

basil99

Active Member
Licensed User
Another tip wanted )

Lets say, I read file into bytes array:

Dim MyArray() As Byte()


Assume, first 4 bytes are representation of some Int variable ( that is 4 bytes long ). So if i want to know this value, i need to calculate it from bytes:

MyInt = MyArray(0) + 256* MyArray(1) + 65536*MyArray(2) + 256*65536*MyArray(2)

May be you know another elegant way for this ?
 

basil99

Active Member
Licensed User
Ok, so

1) Copy binary from DirAssets to another folder
2) Read binary via RAF
3) Delete copy

if i got it right
 

zsezo

Member
Licensed User
I have a problem too with read/write file and or EncodeBase64/DecodeBase64

Can I ask somebody to explain what I missed?

I tried to encode the file on my side, and immediatelly decode back just for test. But the two file is significantly different!

What did I do wrong?

B4X:
    Dim su As StringUtils
    Dim out As OutputStream
    Dim out2 As InputStream
    Dim str1 As String
 
    out.InitializeToBytesArray(1024)
    File.Copy2(File.OpenInput(File.DirRootExternal, "sign18112401.png"), out)
   
    str1=su.EncodeBase64(out.ToBytesArray)
 
    out2.InitializeFromBytesArray(su.DecodeBase64(str1),0,str1.Length)
    File.Copy2(out2,File.OpenOutput(File.DirRootExternal, "sign18112401_b.png",False))
Tia!
 
Top