Get playlist by path - Android android.database

Android examples for android.database:Cursor

Description

Get playlist by path

Demo Code

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

public class Main {

  public static int idFortrack(Context context, String path) {
    Cursor c = query(context, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
        new String[] { MediaStore.Audio.Media._ID }, MediaStore.Audio.Media.DATA + "=?", new String[] { path },
        MediaStore.Audio.Media.DATA);
    return intFromCursor(c);
  }/* w w  w.ja  v  a  2  s.c o m*/

  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, 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