title: Set a selected audio file as ringtone
body: <p>I am developing an application: I am trying to set an audio file as a ringtone.<br>
  I saw many posts, but no one actually helped me, so I decided to ask this question.</p>
  
  <p>I use this code when the Button is clicked:</p>
  
  <pre><code>@Override
      public void onClick(View v) {
  Intent intent = new Intent();  
              intent.setAction(Intent.ACTION_GET_CONTENT);  
              intent.setType("audio/*");          
              startActivityForResult(Intent.createChooser(intent, "Choose Sound File"), Audio);
  }
  </code></pre>
  
  <p>in <code>onActivityResult</code> I am trying to get the file path and then set the audio file as ringtone using the code below:</p>
  
  <pre><code>@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == Audio &amp;&amp; requestCode==RESULT_OK) {
  
  
            Uri s1 = data.getData();
            String s = s1.getPath(); 
  
  
          if(s!=null){    
  
              try {
                  k = new File(new URI(s)); //(File k;)
              } catch (URISyntaxException e) {
  
                  e.printStackTrace();
              }
                  ContentValues values = new ContentValues();
                  values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
                  values.put(MediaStore.MediaColumns.TITLE, "My Song title");
                  values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
                  values.put(MediaStore.Audio.Media.ARTIST, "Some Artist");
                  values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
                  values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
                  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 = getContentResolver().insert(uri, values);
  
                  RingtoneManager.setActualDefaultRingtoneUri(
                    MainActivity.this,
                    RingtoneManager.TYPE_RINGTONE,
                    newUri);   
  
          }
  
          }
  }
  </code></pre>
  
  <p>Unfortunately this code doesn't work.<br>
  I would appreciate your answers. Sorry for my bad English.</p>