【问题标题】:Getting all MP3 files on the external card获取外部卡上的所有 MP3 文件
【发布时间】:2013-03-06 10:06:43
【问题描述】:

我想要实现的是在一个数组列表中获取所有 mp3 文件的 URL 和标题以返回它。全部来自文件夹:/ext_card/Music/ 和所有子文件夹。

我探索的选项似乎都卡住了,我在互联网上查找的所有答案都指向 mediastore,但我不知道如何使用它

并且不要给我 RTFM 废话,因为我找到的有关 mediastore 的解释充其量是迟钝的。

任何人都可以帮助我或给我一个简单的工作媒体存储查询所有 mp3 文件的示例吗?

【问题讨论】:

  • 我只想说这个.....我正在寻找那种问题,这句话:And don't give me the RTFM bullshit, because the explanations i've found for mediastore is retarded at best. 让我笑出声来。谢谢。

标签: android mp3 mediastore


【解决方案1】:

使用它可以帮助您从 SD 卡中获取所有 MP3 歌曲

private static ArrayList<genericSongClass> songs = null;

public void BindAllSongs() {        
        /** Making custom drawable */
        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
        final String[] projection = new String[] {
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.DATA};
                final String sortOrder = MediaStore.Audio.AudioColumns.TITLE
                        + " COLLATE LOCALIZED ASC";

                try {
                    // the uri of the table that we want to query
                    Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                    // query the db
                    cursor = getBaseContext().getContentResolver().query(uri,
                            projection, selection, null, sortOrder);
                    if (cursor != null) {
                        songs = new ArrayList<genericSongClass>(cursor.getCount());
                        cursor.moveToFirst();                       
                        while (!cursor.isAfterLast()) { 
                            GSC = new genericSongClass();
                            GSC.songTitle = cursor.getString(0);
                            GSC.songArtist = cursor.getString(1);   
                            GSC.songData = cursor.getString(2);
                            songs.add(GSC);
                            cursor.moveToNext();
                        }
                    }
                } catch (Exception ex) {

                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                }       

    }

public class genericSongClass {
        String songTitle = "";
        String songArtist = "";
        String songData = "";
        String isChecked = "false";
}

【讨论】:

  • 您的代码将显示所有音乐文件,无论格式如何。如何只获取 .mp3 文件?
【解决方案2】:

这是做这种事情的递归方式,

getMp3s(Arraylist<File> list, File dir){
    File[] files = dir.listFiles();
    for(File file : files){
        if(file.isDirectory()){
            getMp3s(list, file);
        }else{
            //add mp3s to list here
        }
    }
}

【讨论】:

  • 它会给你所有文件的列表,然后你必须过滤掉它。
  • 你的答案不完整,所以我认为,你应该填写所有必要的信息,否则你根本不应该回答
猜你喜欢
  • 1970-01-01
  • 2019-11-22
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 2019-01-27
  • 1970-01-01
  • 2023-03-08
相关资源
最近更新 更多