delete Playlist - Android android.content

Android examples for android.content:ContentResolver

Description

delete Playlist

Demo Code

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

public class Main {

  public static void deletePlaylist(Context context, String playlistName) {
    ContentResolver resolver = context.getContentResolver();
    Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
        lidForplaylist(context, playlistName));
    resolver.delete(uri, null, null);// w w w.  j a  v a2 s.c  o  m

  }

  public static long lidForplaylist(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 longFromCursor(c);
  }

  private static long longFromCursor(Cursor c) {
    long id = -1;
    if (c != null) {
      c.moveToFirst();
      if (!c.isAfterLast()) {
        id = c.getLong(0);
      }
    }
    c.close();
    return id;
  }

  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;
    }
  }

  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);
  }

}

Related Tutorials