Android 多媒体:MediaProvider、MediaStore

MediaProvider

MediaProvider就是Android系统中的一个数据库,又称多媒体数据库,保存了手机上存储的所有文件的信息。类似的还有TelephonyProvider、CalendarProvider、ContactsProvider。
这个数据库存放在/data/data/com.android.providers.media/databases当中,里面有两个数据库:internal.db和external.db,internal.db存放的是系统分区的文件信息,开发者是没法通过接口获得其中的信息的,而external.db存放的则是我们用户能看到的存储区的文件信息,即包含了手机内置存储,还包含了SD卡。
如果你的手机root了,可以使用SQLite Expert 这个软件查看其中的内容。

MediaStore

public final class MediaStore 
extends Object 

MediaProvider提供了在内部和外部存储设备上所有可用媒体的元数据。

内嵌类

  • class MediaStore.Audio
    所有音频内容的容器

  • class MediaStore.Files
    MediaProvider表包含媒体存储中所有文件的索引,包括非媒体文件

  • class MediaStore.Images
    所有图片内容的容器

  • interface MediaStore.MediaColumns
    MediaProvider表的公共字段

  • class MediaStore.Video
    所有视频内容的容器

MediaStore.MediaColumns字段说明

  • String DATA
    磁盘上文件的路径

  • String DATE_ADDED
    文件添加到media provider的时间(单位秒)

  • String DATE_MODIFIED
    文件最后一次修改单元的时间

  • String DISPLAY_NAME
    文件的显示名称

  • String HEIGHT
    图像/视频的高度,以像素为单位

  • String MIME_TYPE
    文件的MIME类型

  • String SIZE
    文件的字节大小

  • String TITLE
    标题

  • String WIDTH
    图像/视频的宽度,以像素为单位。

DEMO

通过MediaProvider读取多媒体数据库中的视频、图片和音频,并显示在RecyclerView中。

  1. 读取视频
    /**
     * 获取视频列表
     */
    public List getVideoList() {
        List list = new ArrayList();
        ContentResolver contentResolver = context.getContentResolver();
        Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String[] projection = null;
        String selection = null;
        //MediaStore.Video.Media.MIME_TYPE + "=? or " + MediaStore.Video.Media.MIME_TYPE + "=?";
        String[] selectionArgs = null;
        //new String[]{"video/mp4", "video/avi"};
        String sortOrder = null;
        //MediaStore.Video.Media.DEFAULT_SORT_ORDER;
        Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
        Cursor thumbCursor = null;
        while (cursor.moveToNext()) {
            MediaInfo videoInfo = new VideoInfo();
            videoInfo.id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
            videoInfo.filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
            videoInfo.mimeType= cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
            videoInfo.title= cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
            videoInfo.addTime=cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DATE_ADDED));
            ((VideoInfo) videoInfo).duration=cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DURATION));
            thumbCursor = contentResolver.query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
                    new String[]{
                            MediaStore.Video.Thumbnails.DATA,
                            MediaStore.Video.Thumbnails.VIDEO_ID
                    },
                    MediaStore.Video.Thumbnails.VIDEO_ID + "=?",
                    new String[]{String.valueOf(videoInfo.id)}, null);

            if (thumbCursor.moveToFirst()) {
                ((VideoInfo) videoInfo).thumbnail = thumbCursor.getString(
                        thumbCursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA));
            }
            list.add(videoInfo);
        }
        thumbCursor.close();
        cursor.close();
        return list;
    }
  1. 读取图片
    public List getImageList() {
        List list = new ArrayList();
        ContentResolver contentResolver = context.getContentResolver();
        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String[] projection = null;
        String selection = null;
        String[] selectionArgs = null;
        String sortOrder = null;
        Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
        Cursor thumbCursor = null;
        while (cursor.moveToNext()) {
            MediaInfo imageInfo = new ImageInfo();
            imageInfo.id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
            imageInfo.filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            imageInfo.mimeType= cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.MIME_TYPE));
            imageInfo.title= cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE));
            imageInfo.addTime=cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED));
            thumbCursor = contentResolver.query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                    new String[]{
                            MediaStore.Images.Thumbnails.DATA,
                            MediaStore.Images.Thumbnails.IMAGE_ID
                    },
                    MediaStore.Images.Thumbnails.IMAGE_ID + "=?",
                    new String[]{String.valueOf(imageInfo.id)}, null);

            if (thumbCursor.moveToFirst()) {
                ((ImageInfo) imageInfo).thumbnail = thumbCursor.getString(
                        thumbCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
            }
            list.add(imageInfo);
        }
        thumbCursor.close();
        cursor.close();
        return list;
    }
  1. 读取音频
    public List getAudioList() {
        List list = new ArrayList();
        ContentResolver contentResolver = context.getContentResolver();
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String[] projection = null;
        String selection = null;
        String[] selectionArgs = null;
        String sortOrder = null;
        Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
        while (cursor.moveToNext()) {
            MediaInfo audioInfo = new AudioInfo();
            audioInfo.id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
            audioInfo.filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
            audioInfo.mimeType= cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE));
            audioInfo.title= cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
            audioInfo.addTime=cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED));
            list.add(audioInfo);
        }
        cursor.close();
        return list;
    }

代码

https://github.com/DavidJi80/Android
v0.5

参考
https://blog.csdn.net/qugename/article/details/53230857
https://developer.android.google.cn/reference/android/provider/MediaStore
https://blog.csdn.net/wbwjx/article/details/70544881
https://blog.csdn.net/mba16c35/article/details/43528389
https://www.cnblogs.com/ldq2016/p/5584971.html

你可能感兴趣的:(Android 多媒体:MediaProvider、MediaStore)