简体   繁体   English

如何使用MediaStore获取存储在android的内部存储和外部存储中的所有音乐文件?

[英]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. 在没有外部存储的设备如Samsung Galaxy S8中,这是一个问题。 I have also tried the cursor with the below uri 我也尝试了以下uri的光标

musicUri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;

This fetches me the device ringtones and SMS tones which are not what I need. 这使我获得了我不需要的设备铃声和SMS铃声。

I have gone through similar questions in StackOverflow and the answers there does not solve this problem for me. 我在StackOverflow中经历了类似的问题,那里的答案并没有为我解决这个问题。

Please help. 请帮忙。 Thanks. 谢谢。

I think the word EXTERNAL is confusing. 我认为EXTERNAL一词令人困惑。 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). 它用于寻址内部存储器扫描过程将把所有音乐输入到媒体数据库,而不管它是在内部还是外部媒体上。(请确保文件夹中没有隐藏的.nomedia文件)。 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(); }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Android中的MediaStore.Files从内部和外部存储中获取所有.pdf文件 - How can I get all .pdf files from Internal as well as External storage using MediaStore.Files in Android 如何从Android的内部和外部存储中获取所有.mp3文件 - How to get all .mp3 files from internal and external storage in android Android - 如何从内部存储中获取所有文件路径或从内部存储中删除所有文件 - Android - How to get all file path from internal storage or delete all files from internal storage 想要列出内部存储和外部存储上的所有音乐文件,但在 Android 10 上无法完成 - Want to list all music file on internal storage and external storage, but it cannot be done on Android 10 如何将文件存储在内置的外部存储中 - How to get files stored in inbuilt external storage 获取内部和外部存储中所有文件夹中的所有文件 - Get all files in all folders in internal and external storage 如何从 Android 的外部存储中加载所有音乐文件? - How can I load all music files from external storage on Android? 如何使用外部应用程序访问存储在内部存储的自定义文件夹中的文件? - How to access files which is stored in custom folder of internal storage using external app? 在Android中使用绝对路径获取内部和外部存储中所有文件(每个文件)的列表 - Get list of all files (every file) in both internal and external storage with absolute path in Android 如何从外部sdcard和手机存储中获取所有文件(移动存储内部)? - How to get all files from external sdcard and phone storage (internal with mobile memory)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM