Android Question Using file.copy2 to create an mp3 file

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

I would like to use select an audio file which is an mp3 file, get the uri for that file, then create another mp3 file in a different location on a phone so it can be used as a ringtone.

So far I use this content chooser coding to get hold of the mp3 file and store the uri for that file into a string.

B4X:
CC.Show("audio/*", "Choose an audio file")

B4X:
Sub ContentChooser_Result (Success As Boolean, Dir As String, FileName As String)

    If Success = True Then
        strUriChosen = FileName
        StartActivity(SoundFiles)
    End If
End Sub

I tried to create a temporary mp3 file with File.Copy2 but it won't compile because it tells me I have an error on the 1st parameter of File.Copy2. It seems it need an InputStream but I don't know how to convert the uri of the audio file to an InputStream.

B4X:
    Dim out As OutputStream
    out = File.OpenOutput(File.DirDefaultExternal, "Temp.mp3" , False )
    File.Copy2(strUriChosen , out)
    out.Close

I'm guessing at this coding because I did not find a tutorial or sample showing how to get this to work.

If someone would be kind enough to upload a sample app showing how to do this, it will be greatly appreciated.

Thanks.
 

DonManfred

Expert
Licensed User
Longtime User
Read the content of the result uri from contentresolver into a byte array
Create a inputstream from this bytearray
Create a outputstream like in your code
Copy the data with file.copy2
 
Upvote 0
Top