46

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.

eebbesen
  • 5,070
  • 8
  • 48
  • 70
Vidar Vestnes
  • 42,644
  • 28
  • 86
  • 100

13 Answers13

73

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

RingtoneManager.setActualDefaultRingtoneUri(
  myActivity,
  RingtoneManager.TYPE_RINGTONE,
  newUri
);  

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.

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Vidar Vestnes
  • 42,644
  • 28
  • 86
  • 100
  • 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`. – escape-llc Oct 19 '11 at 10:49
  • 1
    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.. – Shailendra Madda May 19 '14 at 12:18
  • I used `getApplicationContext()` in place of `main` is it right or what else I can use to solve this issue.. – Shailendra Madda May 20 '14 at 10:00
  • 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. – Aniket Thakur May 03 '15 at 00:46
  • 1
    add below line of code before newUri getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"", null); – AkhilGite Nov 05 '16 at 12:59
  • I want to set default ringtone as none/silence how can I go for this using your code.please help... – Sagar Jan 24 '18 at 11:34
  • Do you have any idea how to set Text as ringtone pls help me here like for a single contact – Sunil Chaudhary Jun 18 '20 at 10:37
4

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) {


                  }
Kishan Kumar
  • 685
  • 1
  • 5
  • 17
  • 1
    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. – dodgy_coder Jul 22 '18 at 02:33
  • 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); – Sumit Garai Jun 08 '19 at 07:32
3
public void setRingtone() {
  String ringtoneuri = Environment.getExternalStorageDirectory().getAbsolutePath() + "/media/ringtone";
  File file1 = new File(ringtoneuri);
  file1.mkdirs();
  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 = fis.read(readData);

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

   fos.close();
  } 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.setStopPreviousRingtone(true);
   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());
  }

 }
SANJAY GUPTA
  • 1,564
  • 13
  • 21
2

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

russoue
  • 5,180
  • 5
  • 27
  • 29
  • 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. – CommonsWare Aug 14 '09 at 01:27
  • 1
    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. – russoue Aug 14 '09 at 13:29
0

I found this code from the Media application from Android.

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

this works form my.

Brais Gabin
  • 5,827
  • 6
  • 57
  • 92
0

This is the code i used! i hope it helps..
This is also the link.

 String exStoragePath =    Environment.getExternalStorageDirectory().getAbsolutePath();
String path=(exStoragePath +"/media/alarms/"); 

saveas(RingtoneManager.TYPE_RINGTONE); 

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,       Uri.parse("file://"+path+filename+".mp3"
  + Environment.getExternalStorageDirectory()))); 


 File k = new File(path, filename);

ContentValues values = new ContentValues(4);   
long current = System.currentTimeMillis();
values.put(MediaStore.MediaColumns.DATA, path + filename  );
values.put(MediaStore.MediaColumns.TITLE,  filename ); 
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");

//new
 values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);  

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

HAPPY CODING!

dondondon
  • 881
  • 8
  • 4
0

I cannot comment the solution because I don't have enough reputation on stack overflow ... I want just add a way to add the audio file into media database without accessing directly to the database and hence avoiding to get duplicates. The solution is based on MediaScannerConnection, this is the code I used:

    String[] files = { audioFullPath };
    MediaScannerConnection.scanFile(
        getApplicationContext(),
        files,
        null,
        new OnScanCompletedListener() {
            @Override
            public void onScanCompleted(String path, Uri uri) {
                Log.v("myapp", "file " + path + " was scanned seccessfully: " + uri);
            }
        }
    );
0

provide intent for ringtone selection.

final Uri currentTone= RingtoneManager.getActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_ALARM);
                Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currentTone);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
                startActivityForResult(intent, 999);

then catch the result of selection in onActivityResult.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 999 && resultCode == RESULT_OK){
            Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            txtView.setText("From :" + uri.getPath());
            //Set selected ringtone here.
            RingtoneManager.setActualDefaultRingtoneUri(
                    this,
                    RingtoneManager.TYPE_RINGTONE,
                    uri
            );
        }
    }
Tatson Baptista
  • 445
  • 3
  • 6
  • 18
0

I have try these code its help

  private void setRingtone(Context context, String path) {
    if (path == null) {
        return;
    }
    File file = new File(path);
    ContentValues contentValues = new ContentValues();
    contentValues.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
    String filterName = path.substring(path.lastIndexOf("/") + 1);
    contentValues.put(MediaStore.MediaColumns.TITLE, filterName);
    contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
    contentValues.put(MediaStore.MediaColumns.SIZE, file.length());
    contentValues.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(path);
    Cursor cursor = context.getContentResolver().query(uri, null, MediaStore.MediaColumns.DATA + "=?", new String[]{path}, null);
    if (cursor != null && cursor.moveToFirst() && cursor.getCount() > 0) {
        String id = cursor.getString(0);
        contentValues.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        context.getContentResolver().update(uri, contentValues, MediaStore.MediaColumns.DATA + "=?", new String[]{path});
        Uri newuri = ContentUris.withAppendedId(uri, Long.valueOf(id));
        try {
            RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newuri);
            Toast.makeText(context, "Set as Ringtone Successfully.", Toast.LENGTH_SHORT).show();
        } catch (Throwable t) {
            t.printStackTrace();
        }
        cursor.close();
    }
}
Mujahid Khan
  • 1,712
  • 1
  • 18
  • 24
0

If the accepted answer is not working then use this:

MediaStore.Audio.Media.INTERNAL_CONTENT_URI

instead of this:

MediaStore.Audio.Media.getContentUriForPath()

while inserting values into the database.

For example :

// Defining ringtone.....
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Sonify");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
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);

// Setting ringtone....
getContentResolver().delete(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,MediaStore.Audio.Media.TITLE + " = \"Sonify\"",null); 
// To avoid duplicate inserts
Uri ringUri = getContentResolver().insert(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, values);
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM, ringUri);

Dont forgot to add

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ʀᴀʜɪʟ
  • 235
  • 1
  • 4
  • 8
0

Use this function to set Ringtone

private void setAsRingtone(String musicId) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (Settings.System.canWrite(this)) {
            Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Long.parseLong(musicId));
            RingtoneManager.setActualDefaultRingtoneUri(
                    this,
                    RingtoneManager.TYPE_RINGTONE,
                    uri
            );
            Toast.makeText(this, "Ring set successfully", Toast.LENGTH_SHORT).show();
        } else {
            Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
            intent.setData(Uri.parse("package:" + getPackageName()));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }
}

Music Id can be obtained from Cursor. Hope you know it, or Check it here

0

That works for me even in android S ,hope its help to you in set ringtone in android programmatically

RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, uri);
Urvish Shiroya
  • 530
  • 1
  • 7
0

if you create file via your bytearray you can set ringtone with file via this function:

public void setRingtone(String filePath) {
        try {
            File f = new File(filePath);
            f.setReadable(true);
            Uri newUri = Uri.parse(f.toString());
            Settings.System.putString(getContentResolver(), Settings.System.RINGTONE,
                    newUri.toString());

        } catch (Exception t) {
        }
    }

ref. for Settings.System.putString() method to set ringtone: https://android.googlesource.com/platform/frameworks/base/+/master/media/java/android/media/RingtoneManager.java#828

0x000000F4
  • 60
  • 8