How to set ringtone using database from device?
<p>I wanted to set a ringtone using the database from my device. My application is able to fetch the ringtone from device and storing it in the database. Now all i want to do is whatever ringtone i select through spinner it set as my default ringtone. </p>

<p>Here is my code </p>

<pre><code>public class SMSReceiver extends BroadcastReceiver{

    DBAdapter db ;
    int vol = 7;
    String code="";
        @Override
    public void onReceive(Context context, Intent intent) {
    contextx=context;
         Bundle bundle = intent.getExtras();
        // TODO Auto-generated method stub
         db = new DBAdapter(context);
         db.open();

         Cursor c = db.getAllTitles();
         int row_size = c.getCount();
         System.out.println("row size++++++++"+row_size);
         if(row_size&gt;0)
         {
             c.moveToFirst();
             code = c.getString(1);
             String status = c.getString(2);
             String ring = c.getString(3);
             vol = c.getInt(4);
             String mode = c.getString(5);

             System.out.println("code++++++++"+code);
             System.out.println("status++++++++"+status);
             System.out.println("ring++++++++"+ring);
             System.out.println("vol++++++++"+vol);
             System.out.println("mode++++++++"+mode);

                   File path = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_RINGTONES);
         File Ringtonefile = new File(path,ring);
         setMyRingtone(Ringtonefile) ;
         }

         Object messages[] = (Object[]) bundle.get("pdus");
            SmsMessage smsMessage[] = new SmsMessage[messages.length];
            for (int n = 0; n &lt; messages.length; n++) {
                smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
            }

            if(smsMessage[0].getMessageBody().equals(""+code))
            {
                AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

                  audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
              audioManager.setStreamVolume(AudioManager.STREAM_RING, vol, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);

            }  
            }

   public void setMyRingtone(File file)  
 {  
   ContentValues values = new ContentValues();  
     values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());  
     values.put(MediaStore.MediaColumns.TITLE, file.getName());
     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);  

     Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());  
     Uri newUri = contextx.getContentResolver().insert(uri, values);  
     RingtoneManager.setActualDefaultRingtoneUri(contextx, RingtoneManager.TYPE_RINGTONE, newUri);} }
</code></pre>
