How to set ringtone in android programmatically?

By | October 5, 2010

This code will set your ringtone for ANDROID.

String filepath ="/sdcard/myring.mp3";
File ringtoneFile = new File(filepath);

To set a ringtone you have to add it to the database.
otherwise it will not be set also gives no error also.

ContentValues content = new ContentValues();
content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
content.put(MediaStore.MediaColumns.TITLE, "test");
content.put(MediaStore.MediaColumns.SIZE, 215454);
content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
content.put(MediaStore.Audio.Media.ARTIST, "artist");
content.put(MediaStore.Audio.Media.DURATION, 230);
content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
content.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
content.put(MediaStore.Audio.Media.IS_ALARM, false);
content.put(MediaStore.Audio.Media.IS_MUSIC, false);

Insert it into the database

Log.i(TAG, "the absolute path of the file is :"+ringtoneFile.getAbsolutePath());
Uri uri = MediaStore.Audio.Media.getContentUriForPath(
ringtoneFile.getAbsolutePath());
Uri newUri = context.getContentResolver().insert(uri, content);
ringtoneUri = newUri;
Log.i(TAG,"the ringtone uri is :"+ringtoneUri);
RingtoneManager.setActualDefaultRingtoneUri(context,
RingtoneManager.TYPE_RINGTONE,newUri);

2 thoughts on “How to set ringtone in android programmatically?

  1. Gunman

    Hello ,
    I Am Newbie In Flutter Framework, I Am Working On Flutter Project In Which I Want To Set MP3 File As A Ringtone For Flutter App’s. If There Is Any Way Let Me Know As Soon As Possible.

    Thanks …

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *