Android examples for android.content:ContentResolver
write Playlist to Context
import java.util.ArrayList; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; public class Main { public static void writePlaylist(Context context, String playlistName, ArrayList<Integer> trackIds) { ContentResolver resolver = context.getContentResolver(); int playlistId = idForplaylist(context, playlistName); Uri uri;//from www .j a v a 2s .com if (playlistId == -1) { ContentValues values = new ContentValues(1); values.put(MediaStore.Audio.Playlists.NAME, playlistName); uri = resolver.insert(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values); playlistId = idForplaylist(context, playlistName); } else { uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId); } // Add all the new tracks to the playlist. int size = trackIds.size(); ContentValues values[] = new ContentValues[size]; for (int k = 0; k < size; ++k) { values[k] = new ContentValues(); values[k].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, k); values[k].put(MediaStore.Audio.Playlists.Members.AUDIO_ID, trackIds.get(k)); } resolver.bulkInsert(uri, values); } public static int idForplaylist(Context context, String name) { Cursor c = query(context, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Audio.Playlists._ID }, MediaStore.Audio.Playlists.NAME + "=?", new String[] { name }, MediaStore.Audio.Playlists.NAME); return intFromCursor(c); } public static Cursor query(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, int limit) { try { ContentResolver resolver = context.getContentResolver(); if (resolver == null) { return null; } if (limit > 0) { uri = uri.buildUpon().appendQueryParameter("limit", "" + limit).build(); } return resolver.query(uri, projection, selection, selectionArgs, sortOrder); } catch (UnsupportedOperationException ex) { return null; } } private static int intFromCursor(Cursor c) { int id = -1; if (c != null) { c.moveToFirst(); if (!c.isAfterLast()) { id = c.getInt(0); } } c.close(); return id; } public static Cursor query(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { return query(context, uri, projection, selection, selectionArgs, sortOrder, 0); } }