有人可以帮我查询Android DB吗? 我需要选择所选文件夹中的所有相册。 对于我制作的所有歌曲:
private Cursor allSongs() {
String[] projection = {MediaStore.Audio.Media._ID, // 0
MediaStore.Audio.Media.ARTIST, // 1
MediaStore.Audio.Media.TITLE, // 2
MediaStore.Audio.Media.ALBUM_ID, // 3
MediaStore.Audio.Media.ALBUM, // 4
MediaStore.Audio.Media.DATA, // 5
MediaStore.Audio.Media.DISPLAY_NAME, // 6
MediaStore.Audio.Media.DURATION, //7
MediaStore.Audio.Media.TITLE_KEY //8
};
String selection = android.provider.MediaStore.Audio.Media.DATA + " like ? ";
String MEDIA_PATH = Environment.getExternalStorageDirectory() + "/Music/";
Cursor musicListSDCardCursor = mQuery(
getActivity(),
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection, selection, new String[]{"%" + MEDIA_PATH + "%"}, null, 0);
return musicListSDCardCursor;
}
和mQuery代码:
public static Cursor mQuery(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;
}
}
但是如何向MediaStore查询以获取此文件夹中的所有专辑/ sdcard / Music /?
我做到了,就像:
private Cursor allAlbums() {
Cursor cursor_albums_from_songs = allSongs();
HashSet<String> albumsList = new HashSet<String>();
int count = cursor_albums_from_songs.getCount();
for (int i = 0; i < count; i++) {
cursor_albums_from_songs.moveToPosition(i);
albumsList.add(cursor_albums_from_songs.getString(3));
Log.i("cursor", "" + cursor_albums_from_songs.getString(3));
}
Log.i("allAlbums", "" + albumsList.size());
ArrayList<String> list = new ArrayList<String>(albumsList.size());
list.addAll(albumsList);
int count_ids = list.size();
Cursor[] cursors = new Cursor[count_ids];
for (int i = 0; i < count_ids; i++) {
String selection = MediaStore.Audio.Albums._ID + " = ? ";
String[] selectionArgs = new String[]{list.get(i)};
String[] projection = {MediaStore.Audio.Albums._ID, //0
MediaStore.Audio.Albums.ALBUM, // 1
MediaStore.Audio.Albums.ALBUM_ART, // 2
MediaStore.Audio.Albums.ARTIST, //3
MediaStore.Audio.Albums.FIRST_YEAR, //4
MediaStore.Audio.Albums.NUMBER_OF_SONGS //5
};
Cursor artistListSDCardCursor = mQuery(
mContext,
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
projection, selection, selectionArgs, null, 0);
cursors[i] = artistListSDCardCursor;
}
MergeCursor mMergeCursor = new MergeCursor(cursors);
return mMergeCursor;
}