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;
    }

至此就获得了音乐信息。

以下是使用ContentResolver查询本地日程重复日程的方法及示例代码: 1. 查询所有日程事件并筛选重复事件 ```java ContentResolver cr = getContentResolver(); Uri uri = CalendarContract.Events.CONTENT_URI; // 查询所有事件 String[] projection = new String[]{ CalendarContract.Events._ID, CalendarContract.Events.TITLE, CalendarContract.Events.DESCRIPTION, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND, CalendarContract.Events.ALL_DAY, CalendarContract.Events.EVENT_LOCATION, CalendarContract.Events.RRULE }; // 筛选重复事件 String selection = CalendarContract.Events.RRULE + " is not null"; // 查询 Cursor cursor = cr.query(uri, projection, selection, null, null); // 遍历结果 while (cursor.moveToNext()) { // 获取事件属性 long eventId = cursor.getLong(0); String title = cursor.getString(1); String description = cursor.getString(2); long startDate = cursor.getLong(3); long endDate = cursor.getLong(4); boolean allDay = cursor.getInt(5) != 0; String location = cursor.getString(6); String recurrenceRule = cursor.getString(7); // 处理重复事件 if (recurrenceRule != null) { // TODO: 处理重复事件 } } // 关闭游标 cursor.close(); ``` 2. 查询指定时间段内的重复事件 ```java ContentResolver cr = getContentResolver(); Uri uri = CalendarContract.Events.CONTENT_URI; // 查询指定时间段内的事件 String[] projection = new String[]{ CalendarContract.Events._ID, CalendarContract.Events.TITLE, CalendarContract.Events.DESCRIPTION, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND, CalendarContract.Events.ALL_DAY, CalendarContract.Events.EVENT_LOCATION, CalendarContract.Events.RRULE }; long startMillis = // 起始时间戳 long endMillis = // 结束时间戳 String selection = "(" + CalendarContract.Events.DTSTART + ">= ? AND " + CalendarContract.Events.DTSTART + "<= ?)"; String[] selectionArgs = new String[]{String.valueOf(startMillis), String.valueOf(endMillis)}; Cursor cursor = cr.query(uri, projection, selection, selectionArgs, null); // 遍历结果 while (cursor.moveToNext()) { // 获取事件属性 long eventId = cursor.getLong(0); String title = cursor.getString(1); String description = cursor.getString(2); long startDate = cursor.getLong(3); long endDate = cursor.getLong(4); boolean allDay = cursor.getInt(5) != 0; String location = cursor.getString(6); String recurrenceRule = cursor.getString(7); // 处理重复事件 if (recurrenceRule != null) { // TODO: 处理重复事件 } } // 关闭游标 cursor.close(); ``` 以上示例代码仅供参考,具体实现方式需要根据具体情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值