Search code examples
javaandroidringtone

android save Ringtone in Sharedpreferences and get it again


I have get the current ringtone . Now I just want to save it in the sharedpreference .

How can I achieve that?

Here the code I tried:

Uri currentRintoneUri = 
          RingtoneManager.getActualDefaultRingtoneUri(context.getApplicationContext(), 
                                                      RingtoneManager.TYPE_RINGTONE);
Ringtone currentRingtone = RingtoneManager.getRingtone(context, currentRintoneUri);

It's not working for ringing tone but it works for edit text :

sharedpreferences = getSharedPreferences(MyPRE, Context.MODE_PRIVATE);
String current = sharedpreferences.getString(CUR, "");
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(CUR, String.valueOf((currentRingtone)));
editor.commit();

Solution

  • Buddy, your code was right. I just wrote a Demo test your code.

    You maybe needed Permission android.permission.READ_EXTERNAL_STORAGE when called this code segment Ringtone currentRingtone = RingtoneManager.getRingtone(context, currentRintoneUri);

    When I called the code segment I got this warning in logcat: W/MediaPlayer: Couldn't open file on client side; trying server side: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media/79645 from pid=8293, uid=11729 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

    So, the right way is to save the String.valueOf(Uri) in SharedPreferences NOT save the String.valueOf(Ringtone object).