title: How to set audio as ringtone programmatically above Android N
body: <p>In the past, we could use code below to set an audio file as ringtone:</p>
  
  <pre><code>ContentValues values = new ContentValues();
  values.put(MediaStore.MediaColumns.DATA, musicFile.getAbsolutePath());
  values.put(MediaStore.MediaColumns.TITLE, "my music");
  values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
  values.put(MediaStore.MediaColumns.SIZE, 215454);
  values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
  values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false); // true for notification sound
  values.put(MediaStore.Audio.Media.IS_ALARM, false);
  values.put(MediaStore.Audio.Media.IS_MUSIC, false);
  
  Uri uri = MediaStore.Audio.Media.getContentUriForPath(musicFile.getAbsolutePath());
  Strint where = MediaStore.MediaColumns.DATA + "=\""
                  + newSoundFile.getAbsolutePath() + "\"";
  getContentResolver().delete(uri, where, null);
  Uri newUri = getContentResolver().insert(uri, values);
  RingtoneManager.setActualDefaultRingtoneUri(
          RingtonesPlaying.this, RingtoneManager.TYPE_RINGTONE, newUri);
  </code></pre>
  
  <p>However, if we run the code above Nougat(7.0, API 24), we will receive a <code>SecurityException</code> for <code>getContentResolver().insert()</code> that we don't have permission of <code>MANAGE_DOCUMENTS</code>, which will always be thrown even if we declare this permission in <code>AndroidManifest</code>.</p>
  
  <p>I really want to set audio file as ringtone since I want users of my app to have the ability of customizing notification sound. In fact we can use <code>builder.setSound(Uri.fromFile(musicFile))</code> before N for <code>Notification</code>, but this approach is also forbidden on N and will throw a <code>FileUriExposedException</code>.</p>