This is my code. It is fetching songs from all directories but i want to get only from specific folder. How can i do it
`
private void readMediaFiles() { ContentResolver contentResolver = context.getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String music = MediaStore.Audio.Media.IS_MUSIC " != 0";
Log.d("this123",music.toString());
String sort = MediaStore.Audio.Media.ALBUM " ASC";
String[] projection = {MediaStore.Audio.Albums.ALBUM_ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA};
Cursor cursor = contentResolver.query(uri,projection,music,null,sort);
if(cursor != null) {
Msg.log(String.valueOf(cursor.getCount()));
if(cursor.getCount() > 0) {
while(cursor.moveToNext()) {
@SuppressLint("Range") String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
@SuppressLint("Range") String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
@SuppressLint("Range") String albumid = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ID));
mediaListModel = new MediaListModel(Long.parseLong(albumid),path,title);
mediaListModelArrayList.add(mediaListModel);
}
}`
CodePudding user response:
You can achieve your goal of fetching only songs from a specific folder by using this approach :
String music = MediaStore.Audio.Media.IS_MUSIC " != 0 AND "
MediaStore.Audio.Media.DATA " LIKE '/mnt/sdcard/Music/SomeArtist/%'";
The /mnt/sdcard/Music/SomeArtist/ is where you want to fetch songs only from there. This limits your managedQuery results to the .../SomeArtist/ dir.
CodePudding user response:
Try this...
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String paths = Environment.getExternalStorageDirectory().getPath();
String music = MediaStore.Audio.Media.IS_MUSIC " !=" 0
" AND " MediaStore.Audio.Media.DATA
" LIKE '" paths
"/Music/BGM/%'";
String sort = MediaStore.Audio.Media.ALBUM " ASC";
String[] projection = {MediaStore.Audio.Albums.ALBUM_ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA};
Cursor cursor = contentResolver.query(uri, projection, music, null, sort);