How can I set a ringtone programmatically android

Midnight picture Midnight · Jun 20, 2013 · Viewed 13.4k times · Source

I have to set ringtone programmatically in android . How can I set a ringtone in android?I search the web but i cant find a good sample any one can help me?

Answer

Sagar Maiyad picture Sagar Maiyad · Jun 20, 2013
ContentValues values = new ContentValues();
   values.put(MediaStore.MediaColumns.DATA, ringFile.getAbsolutePath());
   values.put(MediaStore.MediaColumns.TITLE, "ring");
   values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
   values.put(MediaStore.MediaColumns.SIZE, rindFile.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(ringFile.getAbsolutePath());
   Uri newUri = mCr.insert(uri, values);


   try {
       RingtoneManager.setActualDefaultRingtoneUri(getContext(), RingtoneManager.TYPE_RINGTONE, newUri);
   } catch (Throwable t) {

   }

where ringFile is your file path of your ringtone.