如何制作存储在Android手机中的音乐文件夹的arrayList?

时间:2018-11-23 15:07:42

标签: java arraylist android-mediaplayer android-contentresolver

我制作了具有4个字符串属性(数据,标题,专辑,艺术家)的Audio类

这是将手机上所有音频文件(歌曲)存储在arrayList中的代码。

public void loadAudio(){

    ContentResolver contentResolver = getContext().getContentResolver();

    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
    String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";

    Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);

    if(cursor != null && cursor.getCount() > 0){
        audioList = new ArrayList<>();

        while (cursor.moveToNext()) {
            String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
            String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
            String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
            String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));



            audioList.add(new Audio(data, title, album, artist));
        }
    }
    cursor.close();
}         

如何制作仅包含音乐文件夹名称的arrayList?

0 个答案:

没有答案