Random access files

James Moxham

Member
Licensed User
Longtime User
Can I just check the syntax for random access files?

This is what I have working for media player to play a .wav file off an sd card:
B4X:
    Dim MediaPlayer1 As MediaPlayer
   MediaPlayer1.Initialize( )
    MediaPlayer1.Load("/sdcard", "1200.wav")

and in the autocomplete it says "Load(Dir as string, Filename as string)"

So I tried the same syntax in random files
B4X:
    Dim raf As RandomAccessFile
    raf.Initialize("/sdcard", "packet.wav", False)' open the file

the autocomplete looks very similar "Dir as string, File as string"

and it compiles ok, but when running it gives an error "an error has occurred java.io.filenotfoundexception /sdcard/packet.wav

The file does not yet exist as it has been opened for writing.

Do I need to create this file first? If so, is there a method for doing so within the random library (I can't see one) or do I need to do that from another file library?
 

James Moxham

Member
Licensed User
Longtime User
Thanks TOB. File.DirAssets comes up a lot in example code but I think it is read only

These files are read-only. You can not create new files in this folder (which is actually located inside the apk file).

I tried a few other locations but it is the same problem. I think the issue is because I want to open the file to "write" and most examples open the file to "read".

Maybe I need a routine that checks if a file exists and then if it does not, creates a dummy file with no data, so at least it does then exist?
 
Upvote 0

TOB

Member
Licensed User
Longtime User
I guess this is the one you need:

File.DirDefaultExternal
The default folder for your application in the SD card.
The folder is: <storage card>/Android/data/<package>/files/
It will be created if required.

Note that calling any of the two above properties will add the EXTERNAL_STORAGE permission to your application.

Tip: You can check if there is a storage card and whether it is available with File.ExternalReadable and File.ExternalWritable.
 
Upvote 0

James Moxham

Member
Licensed User
Longtime User
Sorry, maybe I spoke too soon.

It worked once. But now it keeps creating a file of zero length. I have tried deleting the file and even the directory but it recreates the file with zero length again.

B4X:
Sub SaveWavFile
    Dim raf As RandomAccessFile
   Msgbox("Saving to sd card","")
   raf.Initialize(File.DirDefaultExternal, "packet.wav", False)' open the file which will end up in I:\Android\data\test.test.test\files (test.test.test is my program)
   raf.WriteBytes(audiofile,0,30000, 0) ' write bytes. Is 32768 the max size as this is an integer variable
   raf.close
End Sub

I'm not sure what that last parameter in writebytes is supposed to be. It is called "position" but I can't find a description.

And it is a bit of a mystery why it has gone from saving a 30k file to saving one with zero length.

Addit: Yikes, this is going to be hard to debug. Sometimes it saves a 30k file, and sometimes a 0k file. I added this at the end
Msgbox("Finish saving","")
just in case I was pulling the sd card out too early. I have a suspicion though that the msg box comes up before the sd card has finished writing.

Ah, yes I think that is it. I waited a while, and then hit the sd icon on the Android and selected "safely remove card" and then it saved the data.
 
Last edited:
Upvote 0
Top