2016-11-23 9 views
0

私は恐らくURIがどのように機能するかという基本的な誤解を抱いているのでしょうか?私はこのアラーム音を私のSharedPreferencesファイルに保存して、同じ方法でそれを復元しようとしています。SharedPreferencesからメディアプレイヤーに着信音をロードする

私はウリを解析していますどのような問題があると信じて、私はURIが私のsharedpreferencesに次のように格納し

私が試してみました

を取得することだろうかの特に意識していませんよ。

//the displayed name of the ringtone 
RingtoneManager.getRingtone(this, uri).getTitle(this) 

data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) 

それぞれの文字列が正しく復元が、私は私が探しているアラーム音を取得するために解析する必要がある実際のキーを認識していませんよ。

私が見つけた

/** Restore alarm tone and update UI */ 
     if (mSettings.contains(ALARM_TONE)){ 
      alarmTone = mSettings.getString(ALARM_TONE, null); 
      if (alarmTone != null) { 

       uri = Uri.parse(alarmTone); 
       TextView t = (TextView) findViewById(R.id.alarmTone); 
       t.setText(RingtoneManager.getRingtone(this, uri).getTitle(this)); 

       mp = MediaPlayer.create(getApplicationContext(), uri); 
      } 
     } 

答えて

0

ソリューションは好みにウリを保存するためにUri.toString()を使用して、以下のように設定を取得します。バックUri.parse(preferenceString)

onActivityresultを使用して着信音をロードすると、私はすぐに私の好みにここでは、スタートの

/** For selecting an alarmtone */ 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      switch (requestCode) { 
       case ALARM_URI: 
        uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); 
        TextView mTextView = (TextView) findViewById(R.id.alarmTone); 
        mTextView.setText(RingtoneManager.getRingtone(this, uri).getTitle(this)); 

        mp = new MediaPlayer(); 
        mp = MediaPlayer.create(getApplicationContext(), uri); 

        mp.setLooping(true); 

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
        SharedPreferences.Editor editor = settings.edit(); 
        editor.putString(ALARM_TONE, uri.toString()); 
        editor.commit(); 

        break; 

       default: 
        break; 
      } 
     } 
    } 

ロードURI

 if (mSettings.contains(ALARM_TONE)){ 
      alarmTone = mSettings.getString(ALARM_TONE, null); 
      if (alarmTone != null) { 
       uri = Uri.parse(alarmTone); 
       //update textview to loaded alarm tone 
       TextView t = (TextView) findViewById(R.id.alarmTone); 
       t.setText(RingtoneManager.getRingtone(this, uri).getTitle(this)); 

       mp = MediaPlayer.create(getApplicationContext(), uri); 
      } 
     } 
をURI文字列を保存します
関連する問題