Skip to content

Instantly share code, notes, and snippets.

@dzwillpower
Last active August 29, 2015 14:14
Show Gist options
  • Save dzwillpower/9cc9be5f262be4711304 to your computer and use it in GitHub Desktop.
Save dzwillpower/9cc9be5f262be4711304 to your computer and use it in GitHub Desktop.
SQLite 查询语句
public static List<Track> getLocalSongs(Context context, int pageIndex, int pageSize) {
long[] audioId;
Playlist playlist;
List<Track> trackList = new ArrayList<Track>();
String[] projection = new String[] { MediaStore.Audio.Media._ID, };
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
.buildUpon()
.appendQueryParameter(QUERY_PARAMETER_LIMIT, String.valueOf(pageSize))
.appendQueryParameter(QUERY_PARAMETER_OFFSET,
String.valueOf((pageIndex - 1) * pageSize)).build();
Cursor cursor = query(context, uri, projection, null, null, null);
try {
if ((cursor == null) || (cursor.getCount() == 0)) {
MyLog.d(TAG, "cursor is null or count is 0");
return trackList;
}
int len = cursor.getCount();
MyLog.d(TAG, "len: " + len);
audioId = new long[len];
for (int i = 0; i < len; i++) {
cursor.moveToNext();
audioId[i] = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
}
playlist = MusicUtils.arrayToPlaylist(audioId);
if (playlist != null) {
int length = playlist.getPlaylistItems().length;
for (int i = 0; i < length; i++) {
trackList.add(TrackFactory.createTrack(context, playlist.getPlaylistItem(i)));
}
}
} catch (Exception exception) {
exception.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return trackList;
}
public static List<Track> getLocalSongs(Context context, int pageIndex, int pageSize) {
long[] audioId;
Playlist playlist;
List<Track> trackList = new ArrayList<Track>();
String[] projection = new String[] { MediaStore.Audio.Media._ID, };
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
.buildUpon()
.appendQueryParameter(QUERY_PARAMETER_LIMIT, String.valueOf(pageSize))
.appendQueryParameter(QUERY_PARAMETER_OFFSET,
String.valueOf((pageIndex - 1) * pageSize)).build();
int orderId = MusicSettings.getIntPref(context, MusicSettings.PREFERENCE_ALL_SONG_ORDER_BY, 0);
String orderBy = null;
if (orderId == 0) {
orderBy = MediaStore.Audio.Media._ID + " DESC";
} else if (orderId == 1) {
orderBy = MediaStore.Audio.Media.FULL_SPELL;
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(orderBy).append(" limit ").append(String.valueOf(String.valueOf(pageSize)))
.append(" offset ").append(String.valueOf((pageIndex - 1) * pageSize));
Cursor cursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, null, null, stringBuilder.toString());
try {
if ((cursor == null) || (cursor.getCount() == 0)) {
MyLog.d(TAG, "cursor is null or count is 0");
return trackList;
}
int len = cursor.getCount();
MyLog.d(TAG, "len: " + len);
audioId = new long[len];
for (int i = 0; i < len; i++) {
cursor.moveToNext();
audioId[i] = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
}
playlist = MusicUtils.arrayToPlaylist(audioId);
if (playlist != null) {
int length = playlist.getPlaylistItems().length;
for (int i = 0; i < length; i++) {
trackList.add(TrackFactory.createTrack(context, playlist.getPlaylistItem(i)));
}
}
} catch (Exception exception) {
exception.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return trackList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment