如何通过albumID获取专辑的歌曲?

时间:2017-10-15 07:02:54

标签: android android-intent android-recyclerview android-music-player

以下是我通过albumID-

从SD卡获取歌曲的代码

我从另一个玻璃杯中接收了作为意图的albumID并将其与此类的album_id(AlbumsSong.java)进行比较,并且只允许将那些添加到满足代码中给出的条件的数组中。由于某种原因,它只显示我只有一张专辑的歌曲。如果您对这个问题有疑问,请问我!

public class AlbumsSong extends Activity {

RecyclerView recycler_albumSong;
AlbumsSongAdapter albumsSongAdapter;
ArrayList<SongInfoModel> s1 = new ArrayList<>();



@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.album_details);

    long albumId = getIntent().getExtras().getLong("album_id");
    recycler_albumSong = findViewById(R.id.albumsDetails_reyclerView);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    recycler_albumSong.setLayoutManager(linearLayoutManager);
    recycler_albumSong.setHasFixedSize(true);



    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";


    Cursor cursor = getApplicationContext().getContentResolver().query(uri, null, selection, null, null);



    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));

                long album_Id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));


                if(album_Id==albumId){

                SongInfoModel s = new SongInfoModel(name, null, null, null, null, -1, null,null, albumId);
                s1.add(s);

                }

            } while (cursor.moveToNext());
        }


        cursor.close();



    }




    albumsSongAdapter = new AlbumsSongAdapter(getApplicationContext(),s1);
    recycler_albumSong.setAdapter(albumsSongAdapter);

  }

}

0 个答案:

没有答案