当写一些东西(比如音乐播放器)的时候,就需要SD卡的mp3列表了~开始我是使用列文件来着,但是再获取文件的信息(歌手,专辑,图片,时长等。。)很麻烦,所以直接用这个函数了~
1 private void getMusicPaths(){ 2 // 3 // 获取音乐列表 4 // 5 Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); 6 if(cursor.moveToFirst()){ 7 while (!cursor.isAfterLast()) { 8 //歌曲编号 9 //int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)); 10 //歌曲标题 11 String tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)); 12 //歌曲的专辑名:MediaStore.Audio.Media.ALBUM 13 //String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)); 14 //歌曲的歌手名: MediaStore.Audio.Media.ARTIST 15 String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); 16 //歌曲文件的路径 :MediaStore.Audio.Media.DATA 17 String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); 18 //歌曲的总播放时长 :MediaStore.Audio.Media.DURATION 19 //int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); 20 //歌曲文件的大小 :MediaStore.Audio.Media.SIZE 21 //Long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)); 22 cursor.moveToNext(); 23 } 24 } 25 }
Android - 通过系统数据库获取SD卡音乐媒体信息
时间: 2024-11-06 10:53:10