如何从ACTION_RINGTONE_PICKER的意图中提取Uri

时间:2018-10-04 14:18:15

标签: android android-intent ringtonemanager

我最近开始使用Android Studio 3.1.2和SDK 19编码我的第一个android项目。

我的一个片段通过Intent使用Intent(RingtoneManager.ACTION_RINGTONE_PICKER)打开一个铃声选择器,因此用户可以选择一个铃声,然后Uri将其保存到SharedPreferences。选择器将按预期方式打开,但是在选择铃声并按OK后,我收到此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

获取选择器的结果时,我将结果的意图传递给我的SettingsHandler,其中setNotificationRingtone()应注意以下选定的音调:

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")));
}

如果我做对了,我就会以某种方式误解了data方法的意图onActivityResult()的结构。我试图在这里找到一个可以理解的答案,但没有一个与我的问题真正匹配。

如果有人能解释,我将不胜感激,如何从Intent中正确提取结果,以及如何从Intent到Uri解析相应的数据。谢谢。

1 个答案:

答案 0 :(得分:1)

来自文档:https://developer.android.com/reference/android/media/RingtoneManager.html#ACTION_RINGTONE_PICKER

  

输出:EXTRA_RINGTONE_PICKED_URI。

因此将data.getStringExtra("uriString")替换为data.getStringExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI)

验证将调试器断点放入onActivityResult中,并使用调试器检查返回的意图

相关问题