ContentResolver查询音乐目录
2017-07-22 17:09
253 查看
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查询系统音乐文件
- Ubuntu下 Oracle sqldeveloper中文目录、文件,select查询结果中:中文乱码
- 【GBT28181开发:SIP协议实践】之设备目录查询
- 微信公众平台开发应用(天气预报、股票查询、手机归属地查询、在线听音乐、翻译、成绩查询等功能)代码分享如下
- Android利用ContentResolver查询的三种方式
- IO笔记第六发简单文件夹及文件目录查询
- FilenameFilter查询文件和目录
- lucene3.0代码实例(二)-多目录查询
- 查询目录下包含特定字符的文件
- 表达式目录树——自定义LINQ Provider实现LINQ to LDAP查询(其二)
- python从共享目录读取考勤数据,通过openpyxl解析excel2007,查询出自己考勤记录
- lucene5--多索引目录查询以及多线程查询
- 图书搜索接口可查询图书分类目录
- 使用fuser查询文件、目录、socket端口的占用进程
- C#web启用目录查询功能
- 自制光盘目录保存与查询软件
- 带有隶属关系的目录树查询start with
- c#ftp操作全解:创建删除目录,上传下载文件,删除移动文件,文件改名,文件目录查询
- 使用T-SQL进行活动目录查询
- Linux2文件目录操作3-文件内容查询