Search code examples
androidandroid-intentringtonemanager

How to extract a Uri out of an Intent from ACTION_RINGTONE_PICKER


I recently started coding my really first android project using Android Studio 3.1.2 and SDK 19.

One of my Fragments opens a Ringtone picker by Intent using Intent(RingtoneManager.ACTION_RINGTONE_PICKER), so user can select a ringtone, which Uri shall then saved to SharedPreferences. The picker opens as expected, but after I select a ringtone and press ok, i get this RuntimeException:

java.lang.RuntimeException: Failure delivering result ResultInfo
{who=null, request=65537, result=-1, data=Intent { (has extras) }} to activity 
{com.procra.myProject/com.procra.myProject.Activities.MainActivity}: 
java.lang.NullPointerException: uriString

When taking the result of the picker I pass the Intent of the result to my SettingsHandler, where setNotificationRingtone() should take care about the selected tone as below:

public synchronized static void setNotificationRingtone(Context context, Intent data) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(NOTIFICATION_SETTINGS, Context.MODE_PRIVATE);
    sharedPreferences.edit().putString(NOTIFICATION_RINGTONE_URI , data.getStringExtra("uriString")).apply();
    //this is the one I suspect to be the troublemaker
    RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION, Uri.parse(data.getStringExtra("uriString")));
}

If I got that correct I somehow misunderstood the structure of the Intent data the onActivityResult()method returns. I tried to find an understandable answer here, but none of them really matched to my question.

I would appreciate it, if someone can explain, how to extract the result out of the Intent correctly and how to parse the according data from the intent to a Uri. Thanks in forward.


Solution

  • from docs: https://developer.android.com/reference/android/media/RingtoneManager.html#ACTION_RINGTONE_PICKER

    Output: EXTRA_RINGTONE_PICKED_URI.

    so replace data.getStringExtra("uriString") with data.getStringExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI)

    to verify put a debugger-breakpoint into onActivityResult and use the debugger to inspect returned intent