title: How to create a new Playlist using ContentResolver
body: <p>I'm attempting to create a new <code>Playlist</code>, using Android's <code>ContentResolver</code>, that will be added to music player's playlists, but when the code runs, the insert into the playlist returns null (for the <code>Uri</code>) and when I check the music player's playlists, my new <code>Playlist</code> entry isn't there.  I suspect that the reason that the <code>insert()</code> returns null is because I haven't created the Playlist correctly.  Could someone clarify how to dynamically create a new playlist, given that my code doesn't work.  (In my searching, I've found several way to query playlists, but nothing actually creates a new one)</p>
  
  <p>Here's my code...</p>
  
  <pre><code>    ContentResolver resolver = getActivity().getContentResolver();
  
      Uri playlists = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
  
      Log.d(A.TAG, "Checking for existing playlist for " + chart.getName());
      Cursor c = resolver.query(playlists, new String[] {"*"}, null, null, null);
      long playlistId = 0;
      c.moveToFirst();
      do {
          String plname = c.getString(c.getColumnIndex(MediaStore.Audio.Playlists.NAME));
          if (plname.equalsIgnoreCase(chart.getName())) {
              playlistId = c.getLong(c.getColumnIndex(MediaStore.Audio.Playlists._ID));
              break;
          }
      } while (c.moveToNext());
      c.close();
  
      if (playlistId!=0) {
          Uri deleteUri = ContentUris.withAppendedId(playlists, playlistId);
          Log.d(A.TAG, "REMOVING Existing Playlist: " + playlistId);
  
          // delete the playlist
          resolver.delete(deleteUri, null, null);
      }
  
      Log.d(A.TAG, "CREATING PLAYLIST: " + chart.getName());
      ContentValues v = new ContentValues();
      v.put(MediaStore.Audio.Playlists.NAME, chart.getName());
      v.put(MediaStore.Audio.Playlists.DATE_MODIFIED, System.currentTimeMillis());
      Uri newpl = resolver.insert(playlists, v);
      Log.d(A.TAG, "Added PlayLIst: " + newpl);
  
      Uri insUri = Uri.withAppendedPath(newpl, MediaStore.Audio.Playlists.Members.CONTENT_DIRECTORY);
  
      int order = 1;
      Log.d(A.TAG, "Playlist Members Url: " + insUri);
      c = getContentManager().queryWhere(getActivity(), Song.class, Song.Fields.LIBRARYID + " != 0 and " + Song.Fields.CHARTID + " = " + chart.getId(), (String[])null);
      if (c.moveToFirst()) {
          Log.d(A.TAG, "Adding Songs to PlayList **");
          do {
              long id = c.getLong(c.getColumnIndex(Song.Fields.LIBRARYID));
              ContentValues cv = new ContentValues();
              cv.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, order++);
              cv.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, id);
              Uri u = resolver.insert(insUri, cv);
              Log.d(A.TAG, "Added Playlist Item: " + u + " for AUDIO_ID " + id);
          } while (c.moveToNext());
      }
      c.close();
  </code></pre>
  
  <p><em><strong></em> UPDATE: Partially Solved **</strong></p>
  
  <p>The above code does correctly add a new Playlist on <code>4.0.3</code>, but not on <code>2.3</code>.  The only problem areas for <code>4.0.3</code> was that I needed to make sure the <code>DATE_MODIFIED</code> was set on the Playlist and that <code>PLAY_ORDER</code> was set on the Playlist item.  </p>
  
  <p>I still have no idea why it would not create a playlist on 2.x, so if anyone has any thoughts on that, I'd like to know.</p>