zoukankan      html  css  js  c++  java
  • MediaStore---强大的android系统媒体库

    做音乐播放器的时候,第一反应是写一个函数全盘扫描得到所有的mp3文件,但是效率太低,在网上查了一下,发现一个强大的类--MediaStore,特此记录学习。

    The Media provider contains meta data for all available media on both internal and external storage devices.

    MediaStore:存放多媒体信息库

    内部类

    1.class--MediaStore.Audio:Container for all audio content.

    2.class--MediaStore.Files:Media provider table containing an index of all files in the media storage, including non-media files.

    3.class--MediaStore.Images:Contains meta data for all available images.

    4.interface--MediaStore.MediaColumns:Common fields for most MediaProvider tables 

    5.class--MediaStore.Video

    使用:

    MediaStore这个类是android系统提供的一个多媒体数据库,android中多媒体信息都可以从这里提取。这个MediaStore包括了多媒体数据库的所有信息,包括音频,视频和图像,android把所有的多媒体数据库接口进行了封装,所有的数据库不用自己进行创建,直接调用利用ContentResolver去掉用那些封装好的接口就可以进行数据库的操作了。

    eg:

    public List<ScanInfo> searchAllDirectory() {
            List<ScanInfo> list = new ArrayList<ScanInfo>();
            StringBuffer sb = new StringBuffer();
            String[] projection = { MediaStore.Audio.Media.DISPLAY_NAME,
                    MediaStore.Audio.Media.DATA };
            Cursor cr = context.getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, null,
                    null, MediaStore.Audio.Media.DISPLAY_NAME);
            String displayName = null;
            String data = null;
            while (cr.moveToNext()) {
                displayName = cr.getString(cr
                        .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                data = cr.getString(cr.getColumnIndex(MediaStore.Audio.Media.DATA));
                data = data.replace(displayName, "");// 替换文件名留下它的上一级目录
                if (!sb.toString().contains(data)) {
                    list.add(new ScanInfo(data, true));
                    sb.append(data);
                }
            }
            cr.close();
            return list;
        }

    方法解析:

    Cursor query(Uri uri ,String[]projection, String selection, String [] selectionArgs, String sortOrder)

    Parameters:

    uri Uri: The URI, using the content:// scheme, for the content to retrieve.
    projection String: A list of which columns to return. Passing null will return all columns, which is inefficient.
    selection String: A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI.
    selectionArgs String: You may include ?s in selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. The values will be bound as Strings.
    sortOrder String: How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.

      Uri:这个Uri代表要查询的数据库名称加上表的名称。这个Uri一般都直接从MediaStore里取得,例如我要取所有歌的信息,就必须利用MediaStore.Audio.Media. EXTERNAL _CONTENT_URI这个Uri。专辑信息要利用MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI这个Uri来查询,其他查询也都类似。

         Prjs:这个参数代表要从表中选择的列,用一个String数组来表示。

         Selections:相当于SQL语句中的where子句,就是代表你的查询条件。

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

         Order:说明查询结果按什么来排序。

     

  • 相关阅读:
    设计模式----单例模式
    C++ 派生类
    C++ 操作符
    构造,清理,拷贝和移动
    php的yii框架开发总结10
    php的yii框架开发总结9
    php的yii框架开发总结8
    php的yii框架开发总结7
    php的yii框架开发总结6
    php的yii框架开发总结5
  • 原文地址:https://www.cnblogs.com/mbp-study/p/6600683.html
Copyright © 2011-2022 走看看