Chinaunix首页 | 论坛 | 博客
  • 博客访问: 347107
  • 博文数量: 26
  • 博客积分: 169
  • 博客等级: 入伍新兵
  • 技术积分: 516
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-29 23:53
个人简介

爱工作,爱生活,更爱自由的程序媛,坚持最初的梦想,做最好的自己!

文章分类

全部博文(26)

文章存档

2014年(16)

2013年(3)

2012年(7)

我的朋友

分类: Android平台

2014-03-07 15:56:54

这篇博文要跟大家介绍的就是获取媒体库中的音乐,上篇跟大家分享了联系人的获取,其实获取媒体库中的音乐也是类似的,正所谓一通百通。
1、先看我们这次的URI是:MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
注意看EXTERNAL_CONTENT_URI是外部存储器上的,与之相对应的是:INTERNAL_CONTENT_URI
那么我们看下源码是怎么介绍这两种模式的(总之搞不懂就看源码):

  1. /**
  2.              * The content:// style URI for the internal storage.
  3.              */
  4.             public static final Uri INTERNAL_CONTENT_URI =
  5.                     getContentUri("internal");

  6.             /**
  7.              * The content:// style URI for the "primary" external storage
  8.              * volume.
  9.              */
  10.             public static final Uri EXTERNAL_CONTENT_URI =
  11.                     getContentUri("external");
2、然后我们再看是根据什么排序的,原来是默认的排序方式:DEFAULT_SORT_ORDER
   除此之外,还有其它的排序方式,大家可以进去看,进去学习,这里我就不多举例了。
3、另外再啰嗦一句,关于ContentResolver的使用,它是用来获取系统数据库中的数据的,跟ContentProvider类似,只是ContentProvider是共享数据。
   再看下query里面放的是什么参数,我们可以看下源码:
   我一直觉得Cursor是个很重要的东西,它牵涉到了数据库的使用,包括以后要用到Sqlite数据的查询、插入、更新、删除等。
   在以后的博文中我会在详细写下Android四种存储类型的使用,并且会详细讲解Sqlite的使用,希望大家会关注我的博文
   
  1. * @param uri The URI, using the content:// scheme, for the content to
  2.      * retrieve.
  3.      * @param projection A list of which columns to return. Passing null will
  4.      * return all columns, which is inefficient.
  5.      * @param selection A filter declaring which rows to return, formatted as an
  6.      * SQL WHERE clause (excluding the WHERE itself). Passing null will
  7.      * return all rows for the given URI.
  8.      * @param selectionArgs You may include ?s in selection, which will be
  9.      * replaced by the values from selectionArgs, in the order that they
  10.      * appear in the selection. The values will be bound as Strings.
  11.      * @param sortOrder How to order the rows, formatted as an SQL ORDER BY
  12.      * clause (excluding the ORDER BY itself). Passing null will use the
  13.      * default sort order, which may be unordered.
  14.      * @return A Cursor object, which is positioned before the first entry, or null
  15.      * @see Cursor
  16.      */
  17.     public final Cursor query(Uri uri, String[] projection,
  18.             String selection, String[] selectionArgs, String sortOrder) {
  19.         return query(uri, projection, selection, selectionArgs, sortOrder, null);
  20.     }

4、以下就是获取媒体音乐库的信息了,相信大家应该都能看懂的,在这里就不详细说了。

  1. public static void readDataFromSD(List<MusicInfo> songList, Context context) {
  2.         Cursor cursor = context.getContentResolver().query(
  3.                 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
  4.                 MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
  5.         
  6.         if (cursor != null)
  7.             if (cursor.moveToFirst()) {
  8.                 do {
  9.                     if (cursor.getString(
  10.                                     cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA))
  11.                             .endsWith("MP3")
  12.                             || cursor
  13.                                     .getString(
  14.                                             cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA))
  15.                                     .endsWith("mp3")) {
  16.                         MusicInfo songInfo = new MusicInfo();
  17.                         songInfo.setmID(cursor.getInt(cursor
  18.                                 .getColumnIndexOrThrow(MediaStore.Audio.Media._ID)));
  19.                      
  20.                         if (cursor
  21.                                 .getString(cursor
  22.                                         .getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)) != null) {
  23.                             songInfo.setmDuration(cursor.getInt(cursor
  24.                                     .getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
  25.                         }
  26.                     
  27.                         if (cursor
  28.                                 .getString(cursor
  29.                                         .getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)) != null) {
  30.                             float temp = cursor
  31.                                     .getInt(cursor
  32.                                             .getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)) / 1024f / 1024f;
  33.                             String songSize = String.format("%.2f", temp);
  34.                             songInfo.setmSize(songSize + "M");
  35.                         }
  36.                         String path = cursor
  37.                                 .getString(cursor
  38.                                         .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
  39.                         songInfo.setmData(path);
  40.                     
  41.                         String artist = cursor
  42.                                 .getString(cursor
  43.                                         .getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
  44.                         songInfo.setmName(getName(path));
  45.                         songInfo.setmArtist(toGb2312(artist));
  46.                         songList.add(songInfo);
  47.                     }
  48.                 }while (cursor.moveToNext());
  49.             }
  50.         if (cursor != null)
  51.             cursor.close();
  52.     }

5、同理:获取媒体库中的音乐就是用这种方式获取的。

  1. ArrayList cameraList = new ArrayList();
  2. public void getColumnData() {
  3.         String[] projection2 = { "_id", "_data" };
  4.         Uri uri2 = MediaStore.Images.Media.getContentUri("external");
  5.         Cursor c2 = Media.query(getContentResolver(), uri2, projection2, null,
  6.                 MediaStore.Images.Media.DATE_ADDED);
  7.       
  8.         if (c2 != null) {
  9.             if (c2.moveToFirst()) {
  10.                 do {
  11.                     String image_path = c2.getString(c2
  12.                             .getColumnIndexOrThrow("_data"));
  13.                         PicInfo pic = new PicInfo();
  14.                         pic.path = image_path;
  15.                         cameraList.add(pic);
  16.                   
  17.                 } while (c2.moveToNext());
  18.             }
  19.             c2.close();
  20.         }
  21.     }

总结:
这个获取媒体图片、音乐的方式和联系人一样,所以不管以后类似这样的东西,我相信都能很好的解决了。
做开发的,最重要的是举一反三,我们不可能什么都记住,API里那么多类,那么多方法,所以要多去看看源码,如果还没有下载源码的就去下载,
下载了就自己导进去
有了源码就能够更为方便的知道里面的机制了。

PS: 这次就讲这么多了,至于视频资源的获取,我特意留在了下次,为什么不一次写完呢,当然是有我的原因的具体的请看下回分析。
     如果大家有什么好的建议欢迎提出,很高兴和大家分享。O(∩_∩)O哈哈~


阅读(4314) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

云少嘎嘎嘎2014-03-26 16:43:10

cursor.getString( cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)).endsWith("MP3") 这里用MediaStore.Audio.Media.MIME_TYPE做判断应该能好一些