简体   繁体   English

如何从Android的内部和外部存储中获取所有.mp3文件

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

i want to make a music player. 我想做一个音乐播放器。 but i couldn't get all the .mp3 files from internal and external storage. 但是我无法从内部和外部存储中获取所有.mp3文件。 can anyone help me please. 谁能帮我。 Thanks in advance. 提前致谢。 here is my code. 这是我的代码。

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

Please find the function below which is able to get the all mp3 songs which you want. 请找到下面的功能,该功能能够获取您想要的所有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.

相关问题 如何从 android 的内部存储中获取 .mp3 文件 - how to fetch .mp3 files from internal storage in android Android-从内部存储列出listview中的mp3文件 - Android -Listing mp3 files in listview from internal storage 从内部存储android播放.mp3 - Playing .mp3 from internal storage android 如何在Android中从内部和外部SD卡播放mp3文件? - How to play mp3 files from internal and external SD card in android? 在android中查找存储中的所有mp3文件 - Find all mp3 files in storage in android 将mp3文件从内部存储移动到外部存储。 文件不再被识别 - Moved mp3 files from internal to external storage. Files are not recognized anymore 如何找到存储在手机内部存储器中的 all.mp3 文件? - How can I find all .mp3 files stored on the internal storage of my phone? Android - 如何从内部存储中获取所有文件路径或从内部存储中删除所有文件 - Android - How to get all file path from internal storage or delete all files from internal storage 如何使用MediaStore获取存储在android的内部存储和外部存储中的所有音乐文件? - How to get all music files stored in internal storage and external storage in android using MediaStore? Android:SD卡/内部存储中的.mp3无法播放 - Android: .mp3 from sdcard/internal storage not playing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM