Friday, February 10, 2012

How to set a ringtone through code in android?

To set any audio file as ringtone it should be stored into database. //then only it is setted otherwise it wouldn't give any error but not set as default ringtone. //you can add more details BY content.put() method. 
String filepath ="/sdcard/play2.mp3";
File ringtoneFile = new File(filepath);

ContentValues content = new ContentValues();
content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
content.put(MediaStore.MediaColumns.TITLE, "chinnu");
content.put(MediaStore.MediaColumns.SIZE, 215454);
content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
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);

1 comment:

  1. I am not able to set ringtone through code.I want to set the ringtone from my music list but it not happening in the setting section ringtone is set but it goes to default or silent when call comes.solution?

    ReplyDelete