如何在listview中获取sdcard歌曲并在android中的listview中显示?

时间:2017-02-10 07:42:34

标签: android listview android-mediaplayer

我已经编写了这段代码,但是当我启动应用程序时,不幸的是它被停止了。我正在使用Android Studio。

public class AllMusicDemo extends AppCompatActivity {
    String []album;
    String []name;
    String []concat;
    ListView listmusic;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_music_demo);
        listmusic= (ListView) findViewById(R.id.listmusic);
        Uri uri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

        Cursor cursor= getContentResolver().query(uri,
                new String[]{MediaStore.Audio.Media.ALBUM,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.COMPOSER,
                        MediaStore.Audio.Media.DURATION,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.SIZE,
                        MediaStore.Audio.Media.DATA}
        , null, null, null);
        album=new String[cursor.getCount()];
        name=new String[cursor.getCount()];
        cursor.moveToFirst();
        for (int i= 0; i< cursor.getCount(); i++){


            album[i]= cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
            name[i]=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
            concat[i]=album[i]+"   "+name[i];
             cursor.moveToNext();
        }
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(AllMusicDemo.this, android.support.v7.appcompat.R.layout.support_simple_spinner_dropdown_item,concat);
        listmusic.setAdapter(arrayAdapter);
    }
}

1 个答案:

答案 0 :(得分:0)

你可以获得这样的歌曲列表:

public static ArrayList<MediaItem> listOfSongs(Context context){
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor c = context.getContentResolver().query(uri, null, MediaStore.Audio.Media.IS_MUSIC + " != 0", null, null);
        ArrayList<MediaItem> listOfSongs = new ArrayList<MediaItem>();
        c.moveToFirst();
        while(!c.isAfterLast()){
            MediaItem songData = new MediaItem();

            String title = c.getString(c.getColumnIndex(MediaStore.Audio.Media.TITLE));
            String artist = c.getString(c.getColumnIndex(MediaStore.Audio.Media.ARTIST));
            String album = c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM));
            long duration = c.getLong(c.getColumnIndex(MediaStore.Audio.Media.DURATION));
            String data = c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA));
            long albumId = c.getLong(c.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
            String composer = c.getString(c.getColumnIndex(MediaStore.Audio.Media.COMPOSER));

            songData.setTitle(title);
            songData.setAlbum(album);
            songData.setArtist(artist);
            songData.setDuration(duration);
            songData.setPath(data);
            songData.setAlbumId(albumId);
            songData.setComposer(composer);
            listOfSongs.add(songData);

            c.moveToNext();
        }
        c.close();
        Log.d("SIZE", "SIZE: " + listOfSongs.size());
        return listOfSongs;
    }

其中MediaItem是一个保存歌曲详细信息的类:

public class MediaItem {
    String title;
    String artist;
    String album;
    String path;
    long duration;
    long albumId;
    String composer;

    @Override
    public String toString() {
        return title;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public String getAlbum() {
        return album;
    }

    public void setAlbum(String album) {
        this.album = album;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public long getDuration() {
        return duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public long getAlbumId() {
        return albumId;
    }

    public void setAlbumId(long albumId) {
        this.albumId = albumId;
    }

    public String getComposer() {
        return composer;
    }

    public void setComposer(String composer) {
        this.composer = composer;
    }
}

现在您只需将收到的歌曲列表绑定到适配器类。