簡體   English   中英

如何從Android的內部和外部存儲中獲取所有.mp3文件

[英]How to get all .mp3 files from internal and external storage in android

我想做一個音樂播放器。 但是我無法從內部和外部存儲中獲取所有.mp3文件。 誰能幫我。 提前致謝。 這是我的代碼。

    public void getListOfSong1(Context context) {
    SongData.cart.clear();
    Cursor c = context.getContentResolver().query(Media.INTERNAL_CONTENT_URI, null, "is_music != 0", null, null);
    c.moveToFirst();
    while (c.moveToNext()) {
        SongData songData = new SongData();
        int _id = c.getColumnIndex(MostAndRecentSongTableHelper.ID);
        String title = c.getString(c.getColumnIndex(MostAndRecentSongTableHelper.TITLE));
        String artist = c.getString(c.getColumnIndex(MostAndRecentSongTableHelper.ARTIST));
        String album = c.getString(c.getColumnIndex(FavoriteSongsTableHelper.ALBUM));
        long duration = c.getLong(c.getColumnIndex(MostAndRecentSongTableHelper.DURATION));
        String data = c.getString(c.getColumnIndex("_data"));
        long albumId = c.getLong(c.getColumnIndex(MostAndRecentSongTableHelper.ALBUM_ID));
        String composer = c.getString(c.getColumnIndex("composer"));
        songData.setId(_id);
        songData.setSongTitle(title);
        songData.setAlbum(album);
        songData.setArtist(artist);
        songData.setDuration(duration);
        songData.setSongPath(data);
        songData.setAlbumId(albumId);
        if (!data.endsWith(".mp3")) {
            if (!data.endsWith(".MP3")) {
            }
        }
        this.listOfSongs.add(songData);
    }
    c.close();
}

請找到下面的功能,該功能能夠獲取您想要的所有mp3歌曲。

public void getMp3Songs() {

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

    cursor = managedQuery(allsongsuri, STAR, selection, null, null);

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                song_name = cursor
                        .getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                int song_id = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.Audio.Media._ID));

                String fullpath = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.DATA));
                fullsongpath.add(fullpath);

                album_name = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ALBUM));
                int album_id = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

                artist_name = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ARTIST));
                int artist_id = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));


            } while (cursor.moveToNext());

        }
        cursor.close();
        db.closeDatabase();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM