ContentResolver查询音乐目录

ContentResolver查询音乐目录

1、首先,需要获得一个ContentResolver对象

ContentResolver contentResolver = this.getContentResolver();

this在这里指的是上下文

2、使用contentResolver的query()方法

首先查看官方文档

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

返回值是一个Cursor对象

官方文档对参数的解释

uri - The URI, using the content:// scheme, for the content to retrieve.
projection - A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data from storage that isn’t going to be used.
selection - 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 - 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 - 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,代表的是你查询的是哪个provider,比如这里要查音乐这部分,那么这个uri就应该是MediaStore.Audio.Media.EXTERNAL_CONTENT_URI

第二个参数projection,该参数是一个字符串数组,选择返回你所要查询的列,在音乐这部分中,常用查询的有

//歌曲名称
MediaStore.Audio.Media.TITLE,
//歌曲时间
MediaStore.Audio.Media.DURATION,
//歌曲专辑
MediaStore.Audio.Media.ALBUM,
//歌曲演唱者
MediaStore.Audio.Media.ARTIST,
//歌曲ID,如果要获得专辑图片,会使用到
MediaStore.Audio.Media._ID,
//歌曲路径,播放歌曲需要用到
MediaStore.Audio.Media.DATA,
//歌曲专辑ID,如果要获得专辑图片,会使用到
MediaStore.Audio.Media.ALBUM_ID
//传入参数时可写为
new String[]{
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM_ID
}

需要注意的是,当这个参数为null时返回的是所有列。

第三个参数selection,表示筛选,类似与数据库查询的where,比如要查询歌曲名称为”See You Again”则该参数写为

//?的内容由第四个参数填入
MediaStore.Audio.Media.TITLE+"=?"

第四个参数selectionArgs和第三个参数配合查询出需要的内容,替换掉第三个参数中的?

new String[]{"See You Again"}

最后一个参数是返回的顺序

//代表按歌曲ID的降序排列,ASC代表升序,默认升序
MediaStore.Audio.Media._ID+"DESC"

3、对返回值Cursor的理解

把cursor想象成一张表格,每一行代表一首歌曲的信息,每一列分别代表着歌曲名,歌曲时间等。下面的代码可以获得从Cursor中获得音乐信息,并以ArrayList的形式返回

private ArrayList getMusicData()
    {
        ArrayList mmusiclist = new ArrayList();
        if(cursor!=null&&cursor.moveToFirst())
        {
            do {
                String mName=cursor.getString(0);
                long mTime = cursor.getLong(1);
                String mAlbum = cursor.getString(2);
                String mArtist = cursor.getString(3);
                long mId = cursor.getLong(4);
                String mData = cursor.getString(5);
                long malbum_id = cursor.getLong(6);
                HashMap a = new HashMap();
                a.put("name", mName);
                a.put("time", mTime);
                a.put("album", mAlbum);
                a.put("artist", mArtist);
                a.put("id", mId);
                a.put("data", mData);
                a.put("album_id", malbum_id);
                mmusiclist.add(a);
            } while (cursor.moveToNext());
        }
        if(!cursor.isClosed())
        {
            cursor.close();
        }
        return mmusiclist;
    }

至此就获得了音乐信息。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值