How to set ringtone in Android from my activity?

how to set ringtone from raw folder in android programmatically
how to set ringtone in android programmatically
how to set ringtone in android source code
how to set notification ringtone in android programmatically
android set ringtone for contact programmatically
how to set as default ringtone from raw folder programmatically in android
how to play ringtone in android programmatically
how to set alarm ringtone in android studio

I'm trying to find a way to set a new default ringtone by code from my Android activity.

I have already downloaded the ringtone into a bytearray.

Finally, I managed to set the default ringtone to one that i downloaded. The download code is not included below, only what was needed to set it as default ringtone.

File k = new File(path, "mysong.mp3"); // path is a file to /sdcard/media/ringtone

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.SIZE, 215454);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
values.put(MediaStore.Audio.Media.DURATION, 230);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);

//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
Uri newUri = this.getContentResolver().insert(uri, values);


Anyway, I do not totally understand what this code is doing.

The Ringtone manager needs a uri to the file that is to be set as new ringtone. But this uri can not be directly to the sdcard like "/sdcard/media/ringtones/mysong.mp3". That does not work!

What you need is the external file uri of the file which could be something like "/external/audio/media/46"

The 46 is the id of the column in the MediaStore database, so thats why you need to add the sdcard file into the database first.

Anyway, how does mediastore maintain its ids? This number can get really high, as you do this operation many times.

Do i need to delete this row my self? Problem is that some times i dont even controll the deleting of the file since it can be deleted directly from the sdcard with a filebrowser.

How to set ringtone in Android from my activity?, Im trying to find a way to set a new default ringtone by code from my Android activityI have already downloaded the ringtone into a byte I'm trying to find a way to set a new default ringtone by code from my Android activity. I have already downloaded the ringtone into a bytearray.

You can use the built-in RingtonePreference class. AndroidGuys has a nice tutorial on this here.

How to set ringtone in Android from Android activity?, This example demonstrates how do I set ringtone in android from android activity.​Step 1 − Create a new project in Android Studio, go to File  Questions: I’m trying to find a way to set a new default ringtone by code from my Android activity. I have already downloaded the ringtone into a bytearray. How to&Answers: Finally, I managed to set the default ringtone to one that i downloaded.

Answer By Vidar is too long and it adds duplicate entries every time you want to set a song as ringtone . Instead you should try this

Uri newUri=Uri.parse("content://media/external/audio/media/"+ID);  
try {
      RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
catch (Throwable t) {


RingtoneManager, Mar 31, 2016 · How To Set a Custom Ringtone on Your Android Phone ? apps open then it will show selected inserted image as app activity background. tell me how I can set an mp3 file that is in the raw folder of my app as a ringtone? Questions: I’m trying to find a way to set a new default ringtone by code from my Android activity. I have already downloaded the ringtone into a bytearray. Answers: Finally, I managed to set the default ringtone to one that i downloaded.

public void setRingtone() {
  String ringtoneuri = Environment.getExternalStorageDirectory().getAbsolutePath() + "/media/ringtone";
  File file1 = new File(ringtoneuri);
  File newSoundFile = new File(ringtoneuri, "myringtone.mp3");

  Uri mUri = Uri.parse("android.resource://globalapps.funnyringtones/raw/sound_two.mp3");

  ContentResolver mCr = this.getContentResolver();
  AssetFileDescriptor soundFile;
  try {
   soundFile = mCr.openAssetFileDescriptor(mUri, "r");
  } catch (FileNotFoundException e) {
   soundFile = null;

  try {
   byte[] readData = new byte[1024];
   FileInputStream fis = soundFile.createInputStream();
   FileOutputStream fos = new FileOutputStream(newSoundFile);
   int i =;

   while (i != -1) {
    fos.write(readData, 0, i);
    i =;

  } catch (IOException io) {

  ContentValues values = new ContentValues();
  values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
  values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
  values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
  values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
  values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
  values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
  values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
  values.put(MediaStore.Audio.Media.IS_ALARM, true);
  values.put(MediaStore.Audio.Media.IS_MUSIC, false);

  Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
  Uri newUri = mCr.insert(uri, values);
  try {
   Uri rUri = RingtoneManager.getValidRingtoneUri(this);
   if (rUri != null)
   RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);
   Toast.makeText(this, "New Rigntone set", Toast.LENGTH_SHORT).show();
  } catch (Throwable t) {
   Log.e("sanjay in catch", "catch exception"+e.getMessage());


How to set ringtone from raw folder in android programmatically, Pixel 3, Pixel3a, Pixel 2 [first two on Android Q, last one on Android Pie], all use various ringtone apps, without issues. best,. f. Last edited 7/26/19. Ringtones can be lots of fun. They uniquely identify your phone’s jingle, especially when you forget to mute your phone and you’re hustling to turn the thing off because everyone in the room is annoyed by your We Will Rock You ringtone. On your Android phone, you can choose which ringtone you want. You can …

I found this code from the Media application from Android.

Settings.System.RINGTONE, ringUri.toString());

this works form my.

Unable to add ringtones to "my sounds", Android Set Ringtone. GitHub Gist: instantly share code, notes, and Android Set Ringtone. Raw. setActualDefaultRingtoneUri(. myActivity,. Select the contact you want to set the ringtone for. Select the three dot menu icon in the top right. Select Set ringtone. Choose the ringtone from the list and select OK. Again, you can set as many of these as you have ringtones for. How to create custom ringtones for an Android phone. Depending on the make and model of your phone, you may have dozens of ringtones or just a couple.

Android Set Ringtone · GitHub, By adding a Settings Activity, you will be given three options as below: to change the Ringtone of the notification along with vibrate option. Scroll down to find the Phone Ringtone option. Select it to open the dialog box to set your ringtone. This could bring up a dialog box that asks you if you want to use the Android System or your stored music to assign your ringtone. For sake of this example, select Android System.

Implementing Preferences Settings Screen in Android, How to see anyone's phone activity in your phone - Duration: 4:57. Tech Tutorial 515,880 Duration: 2:52 Posted: Dec 28, 2015 The first thing you’ll want to do is open either of the above-mentioned apps and find the contact you’d like to set a custom ringtone for. In this case, I’ll be setting a tone for my good friend Barack Obama. We go way back. Just tap the little pencil icon to open the “Edit contact” menu. From there, tap on the three-dot overflow menu in the top right corner. You see the option that reads “Set ringtone”? Yeah, that’s the one we’re looking for. Give it a tap.

How To Set Any Song As Ringtone On Android? Tutorial Video , CHANGE RINGTONE: Select Phone ringtone. Select the desired ringtone, then select OK. Note: For steps to assign a ringtone to a contact, please see the  Hello, Frnds aaj me apko bataonga ke aap kisi bhi song ko Ringtone ya Notifications par kese laga skte hai apne Android Phone par. Please LIKE, SHARE and SUB

  • Interacting with the media content provider in the code above creates a new entry each time. You may want to keep track of the URIs you are generating, so you can avoid re-downloading, and skip right to RingtoneManager.
  • what is main in this line Uri newUri = main.getContentResolver().insert(uri, values); I am getting new Uri as null why I don't no. Please help me..
  • I used getApplicationContext() in place of main is it right or what else I can use to solve this issue..
  • Works fine in Android L. But it kitkat it gives newUri as null. I see following Exception android.database.sqlite.SQLiteConstraintException: column _data is not unique.And this is happening for any audio file I add.
  • add below line of code before newUri getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"", null);
  • Uh, thanks for the shout-out, but RingtonePreference allows the user to choose a ringtone asset for your application to use. It does not set the system ringtone.
  • Ok, may be I missed but the reference does not explicitly tell that. I wonder what would someone do with a ringtone in his application unless the app is handling incoming calls.
  • You need a combination of Vidar's answer and your's here, which should be run when the file already exists on the SD card.
  • If we have the id of the audio file,we could use this instead , Uri uri; if(path_to_the_file.startsWith("/system")){ uri = Uri.parse(MediaStore.Audio.Media.INTERNAL_CONTENT_URI.toString() +"/"+ id); } else uri = Uri.parse(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.toString() +"/"+ id);
  • this is working fine. but, how to do it API>=23?
  • This will help you to set ringtone from raw folder where you have put your all ringtones.It is working fine for me also
  • soundFile = mCr.openAssetFileDescriptor(mUri, "r"); can you please tell me Why you have written this line.
  • what is globalapps here?
  • How can i set it to silent/none as a default ringtone please help