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:
LogCat connected to: 3MSDU15A20002305
--------- beginning of system--------- beginning of main
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (rock) Create, isFirst = true **
** Activity (rock) Resume **
Error occurred on line: 153 (Rock)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
at anywheresoftware.b4a.phone.RingtoneManagerWrapper.AddToMediaStore(RingtoneManagerWrapper.java:94)
at com.italapp.guitarringtones.rock._setringtonebutton_click(rock.java:684)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:703)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:4793)
at android.view.View$PerformClick.run(View.java:19950)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
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:
SMCSAMSHelper.addSTPEvent: cost 0 ms end.
SMCSAMSHelper.smartTrimAddProcessRelation: cost 0 ms end.
acquire_provider pkg:[com.android.providers.media] pid:[] send to pg
(2067)
Error inserting bucket_id=1389444597 artist_id=6 is_notification=false bucket_display_name=0 is_ringtone=true parent=0 format=12297 storage_id=65537 media_type=2 mime_type=audio/* album_id=13 title=viva rock _data=/storage/emulated/0/viva-rock.mp3 is_music=false date_added=1485443818 is_alarm=false _display_name=viva-rock.mp3 title_key= S 9 S ) K E - =
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: files._data(Sqlite code 2067),(OS error - 2:No such file or directory)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1497)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1362)
at com.android.providers.media.MediaProvider.insertFile(MediaProvider.java:3499)
at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:3758)
at com.android.providers.media.MediaProvider.insert(MediaProvider.java:3143)
at android.content.ContentProvider$Transport.insert(ContentProvider.java:236)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:163)
at android.os.Binder.execTransact(Binder.java:446)
Error occurred on line: 154 (Rock)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
at anywheresoftware.b4a.phone.RingtoneManagerWrapper.AddToMediaStore(RingtoneManagerWrapper.java:94)
at com.italapp.guitarringtones.rock._setringtonebutton_click(rock.java:684)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:703)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:4793)
at android.view.View$PerformClick.run(View.java:19950)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
null: 69
FPS: 1
[CDS]close[6777]
[CDS]close[6778]
[CDS]close[6777]
close [socket][/0.0.0.0:6777]
[CDS]close[6778]
close [socket][/0.0.0.0:6778]
setOccluded false
sendKeyguardVisibilityChanged(true)
handleKeyguardVisibilityChanged(1)
handleMessage dt = 0
moveTaskToTop mTaskChanged:true
ssignParent(ViewParent parent) parent is: null
setFocusedApp update app ops, mTaskChanged set to:false
hw_get_module_by_class: lib loaded: /system/lib/hw/gralloc.msm8909.so
setOccluded false
sendKeyguardVisibilityChanged(true)
handleKeyguardVisibilityChanged(1)
handleMessage dt = 0
setOccluded false
sendKeyguardVisibilityChanged(true)
handleKeyguardVisibilityChanged(1)
handleMessage dt = 0
ssignParent(ViewParent parent) parent is: null
acquireWakeLockInternal: lock=173242561, flags=0x1, packageName=android, ws=null, uid=1000, pid=796
updateWakeLockSummaryLocked: mWakefulness=Awake, mWakeLockSummary=0x23
updateUserActivitySummaryLocked: mWakefulness=Awake, mUserActivitySummary=0x1, nextTimeout=4052177907 (in 112800 ms)
updateDisplayPowerStateLocked: mDisplayReady=true, policy=3, mWakefulness=1, mWakeLockSummary=0x23, mUserActivitySummary=0x1, mBootCompleted=true, mScreenBrightnessBoostInProgress=false, useProximitySensor=false, mRequestWaitForNegativeProximity=false, mBrightnessWaitModeEnabled=false
** Activity (rock) Pause, UserClosed = true **
resumeTopActivityLocked: Skip resume: some activity pausing.
System.exit called, status: 0
VM exiting with result code 0, cleanup skipped.
java.lang.NullPointerException: Attempt to invoke virtual method 'int com.huawei.lcagent.client.LogCollectManager.getUserType()' on a null object reference
at huawei.com.android.server.util.ReportTool.getUserType(ReportTool.java:86)
at huawei.com.android.server.util.ReportTool.isBetaUser(ReportTool.java:73)
at huawei.com.android.server.util.ReportTool.report(ReportTool.java:58)
at huawei.com.android.server.util.UserBehaviourRecord.appExitRecord(UserBehaviourRecord.java:65)
at com.android.server.am.ActivityStack.completePauseLocked(ActivityStack.java:1167)
at com.android.server.am.ActivityStack.activityPausedLocked(ActivityStack.java:1083)
at com.android.server.am.ActivityManagerService.activityPaused(ActivityManagerService.java:7008)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:519)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2466)
at com.android.server.am.HwActivityManagerService.onTransact(HwActivityManagerService.java:229)
at android.os.Binder.execTransact(Binder.java:446)
This is not beta user build
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: 436
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

Well-Known 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

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (oynat) Create, isFirst = true **
** Activity (oynat) Resume **
1
false
oynat_button1_click (java line: 1086)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
at anywheresoftware.b4a.phone.RingtoneManagerWrapper.AddToMediaStore(RingtoneManagerWrapper.java:94)
at akay.yazilim.bjkm.oynat._button1_click(oynat.java:1086)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10826)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 
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
Top