android 如何选择音乐文件从一个特定的文件夹,如“我的音乐”在RecyclerView

eh57zj3b  于 2023-01-15  发布在  Android
关注(0)|答案(2)|浏览(147)

这是我的代码。它是从所有目录中获取歌曲,但我想只从特定的文件夹。我怎么做呢
'
私有空读取媒体文件(){内容解析器内容解析器= 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);
            }
        }`
yiytaume

yiytaume1#

您可以使用以下方法实现仅从特定文件夹中提取歌曲的目标:

String music = MediaStore.Audio.Media.IS_MUSIC + " != 0 AND " +
            MediaStore.Audio.Media.DATA + " LIKE '/mnt/sdcard/Music/SomeArtist/%'";

***/mnt/sdcard/Music/SomeArtist/***是您只想从那里获取歌曲的位置。这会将您的managedQuery结果限制到.../SomeArtist/目录。

jchrr9hc

jchrr9hc2#

试试这个...

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

相关问题