Android Question Error using RingtoneManager

Massy

Member
Licensed User
Longtime User
Hello, I'm trying to use RingtoneManager but I'm always getting this error... what could I do wrong?

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference

I'm using this code:
B4X:
Sub setRingtoneButton_Click
    Dim r As RingtoneManager
    Dim u As String
    u = r.AddToMediaStore(File.DirRootExternal, nomeConMp3, nomeFileCheSuona, False, False, True, False)
    r.SetDefault(r.TYPE_RINGTONE, u)
  
End Sub
and the error is on "u = r.AddToMediaStore"....

When I try the mp3 file with MediaPlayer it sounds fine:
B4X:
MediaPlayer1.Load(File.DirAssets, nomeConMp3)
MediaPlayer1.Play

Any help would be highly appreciated as I'm stuck...
 
Last edited:

Massy

Member
Licensed User
Longtime User
Can you post the full error message from the logs?

What is the value of Log(u) ?
This is the full error message:
About the value of Log(u) It doesn't appear at all as the error start on line:
B4X:
u = r.AddToMediaStore(File.DirRootExternal, nomeConMp3, nomeFileCheSuona, False, False, True, False)
so log is not called at all
 
Last edited:
Upvote 0

Massy

Member
Licensed User
Longtime User
Check the unfiltered logs. Maybe there will be more information there.

Can you upload the audio file?
This is the unfiltered log:
I attached a zip file with 2 mp3 one from another app that is 25 sec long and one that is 2 sec both are not working
 

Attachments

  • Files mp3.zip
    267.9 KB · Views: 343
Upvote 0

Massy

Member
Licensed User
Longtime User
What is the output of:
B4X:
Log(nomeConMp3)
Log(File.Exists(File.DirRootExternal, nomeConMp3))
I figured out everything... my mistake was I did not copy the ringtone in DirRootExternal before... now it is working fine.
Thanks a lot for your help Erel
 
Last edited:
Upvote 0

Almora

Active Member
Licensed User
Longtime User
I have the same problem but I get an error.

B4X:
Sub Button1_Click
    Log(1)
    Log(File.Exists(File.DirRootExternal, "1"))
   
    Dim r As RingtoneManager
    Dim u As String


    u = r.AddToMediaStore(File.DirRootExternal, "1.mp3", 1, False, False, True, False)
    r.SetDefault(r.TYPE_RINGTONE, u)

End Sub

 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It will work on the first time you try to add the ringtone. You cannot add the same file multiple times.

You can do something like:
B4X:
Sub Button1_Click
   Dim r As RingtoneManager
   Dim u As String
   If File.Exists(File.DirRootExternal, "1.mp3") = False Then
     File.Copy(File.DirAssets, "1.mp3", File.DirRootExternal, "1.mp3")
   End If
   Try
     u = r.AddToMediaStore(File.DirRootExternal, "1.mp3", 1, False, False, True, False) 'will fail if the file was added in the past.
     File.WriteString(File.DirInternal, "uri.txt", u)
   Catch
     u = File.ReadString(File.DirInternal, "uri.txt") 
   End Try
   r.SetDefault(r.TYPE_RINGTONE, u)
   r.Play(u)
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…