Q:

insert song to playlist returns null

I was trying to build a music player application , and I wanted to add a feature in order to add a song to a playlist.Here is the code:
public static void addToPlaylist(ContentResolver resolver,SongDetails songDetails,int  playlistId) {
    String[] cols = new String[] {
            "count(*)"
    };
    Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
    Cursor cur = resolver.query(uri, cols, null, null, null);
    cur.moveToFirst();
    final int base = cur.getInt(0);
    cur.close();
    ContentValues values = new ContentValues();

    values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, Integer.valueOf(base +   songDetails.getId()));
    values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, songDetails.getId());
    values.put(MediaStore.Audio.Playlists.Members.DISPLAY_NAME, songDetails.getSongTitle());
    values.put(MediaStore.Audio.Playlists.Members.ARTIST, songDetails.getArtistName());
    values.put(MediaStore.Audio.Playlists.Members.DATA, songDetails.getSongData());

    Log.i("URI:",resolver.insert(uri, values)+"");
}

and in the logs I always get:
URI: null

which means that the song is not added to the playlist.Does anybody have any idea why the song is not added?

A:

I've been using this method and it worked for me.  
 public static String addTracksToPlaylist(final long id, List<MediaData> tracks, final Context context) {
    int count = getPlaylistSize(id, context);
    ContentValues[] values = new ContentValues[tracks.size()];
    for (int i = 0; i < tracks.size(); i++) {
        values[i] = new ContentValues();
        values[i].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, i + count + 1);
        values[i].put(MediaStore.Audio.Playlists.Members.AUDIO_ID, tracks.get(i).getId());
    }
    Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", id);
    ContentResolver resolver = context.getContentResolver();
    int num = resolver.bulkInsert(uri, values);
    resolver.notifyChange(Uri.parse("content://media"), null);
    return String.format(context.getString(R.string.ADDED_TO_PLAYLIST), num, context.getString(R.string.CURRENT));
 }

Retrieving tracks from playlist:
public static Cursor getTrackListFromPlaylist(Context context, long plid) {
        String[] MEDIA_COLUMNS = new String[] {
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.ARTIST_ID,
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Playlists.Members.AUDIO_ID,
                MediaStore.Audio.Media.DURATION,
                MediaStore.Audio.Media.DATA,
                MINUTES + "||':'||" + SECONDS + " as " + FORMATTED_DURATION,
                MediaStore.Audio.Media.ALBUM_ID,
                MediaStore.Audio.Media.ALBUM,
                MediaStore.Audio.Media._ID
        };
        return query(context, MediaStore.Audio.Playlists.Members.getContentUri("external", plid),
                MEDIA_COLUMNS, null, null, MediaStore.Audio.Playlists.Members.DEFAULT_SORT_ORDER);
    }

