简体   繁体   中英

How to get all music files stored in internal storage and external storage in android using MediaStore?

I am trying to list all songs in the internal storage and external storage for my application as a list view. I am using the below code for fetching songs

ContentResolver musicResolver = getContentResolver();
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
    if(musicCursor!=null && musicCursor.moveToFirst()){
        //get columns
        int titleColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.TITLE);
        int idColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media._ID);
        int artistColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.ARTIST);

        int durationColumn=musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DURATION);


        do {
            try {
                long thisId = musicCursor.getLong(idColumn);
                String thisTitle = musicCursor.getString(titleColumn);
                String thisArtist = musicCursor.getString(artistColumn);
                String duration = musicCursor.getString(durationColumn);
                if (!thisArtist.equalsIgnoreCase("<unknown>")) {
                    //save track
                }

            } catch (Exception e) {

            }
        }
        while (musicCursor.moveToNext());

The above code is successfully fetching all the songs in the external storage but is not fetching the songs stored in the internal memory. This is a problem in devices like Samsung Galaxy S8 where there is no external storage. I have also tried the cursor with the below uri

musicUri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;

This fetches me the device ringtones and SMS tones which are not what I need.

I have gone through similar questions in StackOverflow and the answers there does not solve this problem for me.

Please help. Thanks.

I think the word EXTERNAL is confusing. It is used to address internal memory The scanning process will enter all your music into the media database irrespective of whether it is on internal or external media.(just make sure there is no hidden .nomedia file in the folder). The code below should return all your music. If it does not then I suspect your media database is not fully populated.

        public Cursor getAllTracks(Context context) {
    // gets all tracks
     Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    ContentResolver cr = context.getContentResolver();
    final String[] columns = {track_id ,track_no, artist, track_name,
            album, duration, path, year, composer};
    return cr.query(uri, columns, null, null, null);
}

this works for me. try this

 Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
    Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));

                SongInfo s = new SongInfo(name, artist, url);
                _songs.add(s);

            } while (cursor.moveToNext());

        }

        cursor.close(); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM