Android系统提供了MediaScanner,MediaProvider,MediaStore等接口,并且提供了一套数据库表格。当手机开机或者有SD卡插拔等事件发生时,系统将会自动扫描SD卡和手机内存上的媒体文件,如audio,video,图片等,将相应的信息放到定义好的数据库表格中。在这个程序中,我们不需要关心如何去扫描手机中的文件,只要了解如何查询和使用这些信息就可以了。

MediaStore中定义了一系列的数据表格,通过ContentResolver提供的查询接口,我们可以得到各种需要的信息。下面我们重点介绍查询SD卡上的音乐文件信息。

先来了解一下ContentResolver的查询接口:
Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder);

Uri:指明要查询的数据库名称加上表的名称,从MediaStore中我们可以找到相应信息的参数

Projection: 指定查询数据库表中的哪几列,返回的游标中将包括相应的信息。Null则返回所有信息。

selection: 指定查询条件

selectionArgs:参数selection里有 ?这个符号是,这里可以以实际值代替这个问号。如果selection这个没有?的话,那么这个String数组可以为null。

SortOrder:指定查询结果的排列顺序

下面的命令将返回所有在外部存储卡上的音乐文件的信息:
Cursor cursor = query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
得到cursor后,我们可以调用Cursor的相关方法具体的音乐信息:
歌曲ID:MediaStore.Audio.Media._ID
Int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));


歌曲的名称 :MediaStore.Audio.Media.TITL
String tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));

歌曲的专辑名:MediaStore.Audio.Media.ALBUM
String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));

歌曲的歌手名: MediaStore.Audio.Media.ARTIST
String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));

歌曲文件的全路径 :MediaStore.Audio.Media.DATA
String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));

歌曲文件的名称:MediaStroe.Audio.Media.DISPLAY_NAME

String display_name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));

歌曲文件的发行日期:MediaStore.Audio.Media.YEAR
String year = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.YEAR));


歌曲的总播放时长 :MediaStore.Audio.Media.DURATION
Int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));

歌曲文件的大小 :MediaStore.Audio.Media.SIZE
Int size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));

其中图片的保存路径在另一个数据库中,需要根据专辑id 进行查询

String[] projection = {MediaStore.Audio.Albums.ALBUM_ART};
        String imagePath = null;
        Uri uri = Uri.parse("content://media" + MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI.getPath() + "/" + albumId);

        Cursor cur = context.getContentResolver().query(uri, projection, null, null, null);
        if (cur == null) {
            return null;
        }

        if (cur.getCount() > 0 && cur.getColumnCount() > 0) {
            cur.moveToNext();
            imagePath = cur.getString(0);
        }
        cur.close();

完整代码

/**
 * Project: MusicPlayer
 * Created by Zdd on 2018/2/2.
 */

public class MediaManager {

    private HashSet<SongInfo> songs;
    private static volatile MediaManager MEDIAMANAGER;

    private MediaManager() {

    }

    //传入 Application Context
    public static MediaManager getInstance() {
        if (MEDIAMANAGER == null) {
            synchronized (MediaManager.class) {
                if (MEDIAMANAGER == null)
                    MEDIAMANAGER = new MediaManager();
            }
        }
        return MEDIAMANAGER;
    }

    public HashSet<SongInfo> refreshData(Context context) {
        if (null == songs) {
            songs = new HashSet<>();
        } else {
            songs.clear();
        }
        Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null,
                null, null);
        if (cursor == null) {
            return songs;
        }
        while (cursor.moveToNext()) {
            SongInfo song = new SongInfo();
            song.setAlbum_id(cursor.getString(cursor.getColumnIndex(SongInfo.ALBUM_ID)));
            song.setAlbum_path(getAlbumArtPicPath(context, song.getAlbum_id()));
            song.setTitle_key(cursor.getString(cursor.getColumnIndex(SongInfo.TITLE_KEY)));
            song.setArtist_key(cursor.getString(cursor.getColumnIndex(SongInfo.ARTIST_KEY)));
            song.setAlbum_key(cursor.getString(cursor.getColumnIndex(SongInfo.ALBUM_KEY)));
            song.setArtist(cursor.getString(cursor.getColumnIndex(SongInfo.ARTIST)));
            song.setAlbum(cursor.getString(cursor.getColumnIndex(SongInfo.ALBUM)));
            song.setData(cursor.getString(cursor.getColumnIndex(SongInfo.DATA)));
            song.setDisplay_name(cursor.getString(cursor.getColumnIndex(SongInfo.DISPLAY_NAME)));
            song.setTitle(cursor.getString(cursor.getColumnIndex(SongInfo.TITLE)));
            song.setMime_type(cursor.getString(cursor.getColumnIndex(SongInfo.MIME_TYPE)));
            song.setYear(cursor.getLong(cursor.getColumnIndex(SongInfo.YEAR)));
            song.setDuration(cursor.getLong(cursor.getColumnIndex(SongInfo.DURATION)));
            song.setSize(cursor.getLong(cursor.getColumnIndex(SongInfo.SIZE)));
            song.setDate_added(cursor.getLong(cursor.getColumnIndex(SongInfo.DATE_ADDED)));
            song.setDate_modified(cursor.getLong(cursor.getColumnIndex(SongInfo.DATE_MODIFIED)));

            songs.add(song);
        }
        cursor.close();

        return songs;
    }

    //根据专辑 id 获得专辑图片保存路径
    private synchronized String getAlbumArtPicPath(Context context, String albumId) {

        // 小米应用商店检测crash ,错误信息:[31188,0,com.duan.musicoco,13155908,java.lang.IllegalStateException,Unknown URL: content://media/external/audio/albums/null,Parcel.java,1548]
        if (!StringUtils.isReal(albumId)) {
            return null;
        }

        String[] projection = {MediaStore.Audio.Albums.ALBUM_ART};
        String imagePath = null;
        Uri uri = Uri.parse("content://media" + MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI.getPath() + "/" + albumId);

        Cursor cur = context.getContentResolver().query(uri, projection, null, null, null);
        if (cur == null) {
            return null;
        }

        if (cur.getCount() > 0 && cur.getColumnCount() > 0) {
            cur.moveToNext();
            imagePath = cur.getString(0);
        }
        cur.close();


        return imagePath;
    }

    /**
     * 获取歌曲信息
     *
     * @param context
     * @param song
     * @return
     */
    public SongInfo getSongInfo(Context context, Song song) {
        check(context);
        SongInfo info = null;
        for (SongInfo s :
                songs) {
            info = s;
            if (info.getData().equals(song.path)) {// 对于同一首歌的路径是相同的
                break;
            }
        }
        return info;
    }

    /**
     * 根据歌曲存放路径 获取歌曲信息
     *
     * @param context
     * @param path
     * @return
     */
    public SongInfo getSongInfo(Context context, String path) {
        return getSongInfo(context, new Song(path));
    }

    /**
     * 获取歌曲列表
     *
     * @param context
     * @return
     */
    public List<Song> getSongList(Context context) {
        check(context);
        List<Song> songInfos = new ArrayList<>();
        for (SongInfo s :
                songs) {
            songInfos.add(new Song(s.getData()));
        }
        return songInfos;
    }

    /**
     * 获取歌曲信息列表
     *
     * @param context
     * @return
     */
    public List<SongInfo> getSongInfoList(Context context) {
        check(context);
        List<SongInfo> songInfos = new ArrayList<>();
        for (SongInfo song : songs) {
            songInfos.add(song);
        }
        return songInfos;
    }

    private void check(Context context) {
        if (songs == null)
            refreshData(context);
    }

    /**
     * 扫描sd卡
     *
     * @param context
     * @param listener
     */
    public void scanSdCard(Context context, MediaScannerConnection.OnScanCompletedListener listener) {
        MediaScannerConnection.scanFile(context, new String[]{
                Environment.getExternalStorageDirectory().getAbsolutePath()
        }, null, listener);
    }

    /**
     * 检查媒体库是否为空
     *
     * @param context
     * @param refresh
     * @return
     */
    public boolean emptyMediaLibrary(Context context, boolean refresh) {

        if (refresh) {
            refreshData(context);
        } else {
            check(context);
        }
        if (songs.size() == 0) {
            return true;
        } else {
            return false;
        }
    }
}