Android Question How to load a file into Byte()?

kimstudio

Active Member
Licensed User
Longtime User
I am struggling with the "seems very simple" task: load a file binary content to byte(). I've searched the forum a lot and found out it is not simple to me at all.

I tried Erel's TextEditor example, if I load a file from a certain directory by file explorer, "Error loading file". However when I click save to default(in my case Download, Android ver. 10 Edit: it is not 11) dir and reload again it works.

I want to load a mp3 file into byte() to do further processing. If I put cc.Show("audio/mpeg", "Choose audio file") and I use another app to select an audio file then all audio files will show, not only with mp3 extension. I then use File.ReadBytes() and it fails.

How to click a button, shows a dialog or file explorer like list, select a file only with mp3 extension, load it into byte() for further processing?
Thanks in advance and I assume a fully working example will help a lot as this is a basic operation for many apps.
 
Last edited:

teddybear

Well-Known Member
Licensed User
It's ok to File.ReadBytes(Result.Dir, Result.FileName), what error is you did that?
 
Upvote 0

kimstudio

Active Member
Licensed User
Longtime User
For TextEditor: if select txt file from another app, "Error loading file"; from the file list in the dialog without another app, it works

For my change for mp3 using following code: click Button2 the app just shuts down. Dim b() as Byte = File.ReadBytes(dir, filename) is same.
dir = ContentDir
filename = content://media/external/audio/media/368884

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim CC As ContentChooser 'Phone Library
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim mp3dir, mp3file As String
    Private Label1 As Label
    Private Label2 As Label
    Private Label3 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
   
    Activity.LoadLayout("Layout1")

    CC.Initialize("CC")
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Button1_Click
'    CC.Show("image/*", "Choose image")
    'CC.Show("audio/*", "Choose audio file")

    CC.Show("audio/mpeg", "Choose audio mp3 file")
    Wait For CC_Result (Success As Boolean, dir As String, filename As String)
    If Success Then
        mp3dir = dir
        mp3file = filename
        Label1.Text = mp3dir
        Label2.Text = mp3file
    End If
End Sub


Private Sub Button2_Click
    Dim ins As InputStream = File.OpenInput(mp3dir, mp3file)
    Label3.Text = ins.BytesAvailable
End Sub
 
Upvote 0

kimstudio

Active Member
Licensed User
Longtime User
It seems whether it works or not depends on which app to use. The returned filenames from different methods are different.


Screenshot_20231015_162817.png
 
Upvote 0

kimstudio

Active Member
Licensed User
Longtime User
After reading several CC related posts again, now with AddPermission (android.permission.READ_EXTERNAL_STORAGE) added, select with third party apps also work.
 
Upvote 0

kimstudio

Active Member
Licensed User
Longtime User
Thanks Erel. I made a new B4A app and didn't use the READ_EXTERNAL_STORAGE permission.
The problem is some 3rd party apps will work and some not for ContentChooser and we can't make sure users will use the right one. I may check later.
 
Upvote 0
Top