这篇博文要跟大家介绍的就是获取媒体库中的音乐,上篇跟大家分享了联系人的获取,其实获取媒体库中的音乐也是类似的,正所谓一通百通。
1、先看我们这次的URI是:MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
注意看EXTERNAL_CONTENT_URI是外部存储器上的,与之相对应的是:INTERNAL_CONTENT_URI
那么我们看下源码是怎么介绍这两种模式的(总之搞不懂就看源码):
-
/**
-
* The content:// style URI for the internal storage.
-
*/
-
public static final Uri INTERNAL_CONTENT_URI =
-
getContentUri("internal");
-
-
/**
-
* The content:// style URI for the "primary" external storage
-
* volume.
-
*/
-
public static final Uri EXTERNAL_CONTENT_URI =
-
getContentUri("external");
2、然后我们再看是根据什么排序的,原来是默认的排序方式:DEFAULT_SORT_ORDER
除此之外,还有其它的排序方式,大家可以进去看,进去学习,这里我就不多举例了。
3、另外再啰嗦一句,关于ContentResolver的使用,它是用来获取系统数据库中的数据的,跟ContentProvider类似,只是ContentProvider是共享数据。
再看下query里面放的是什么参数,我们可以看下源码:
我一直觉得Cursor是个很重要的东西,它牵涉到了数据库的使用,包括以后要用到Sqlite数据的查询、插入、更新、删除等。
在以后的博文中我会在详细写下Android四种存储类型的使用,并且会详细讲解Sqlite的使用,希望大家会关注我的博文。
-
* @param uri The URI, using the content:// scheme, for the content to
-
* retrieve.
-
* @param projection A list of which columns to return. Passing null will
-
* return all columns, which is inefficient.
-
* @param 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.
-
* @param 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.
-
* @param 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.
-
* @return A Cursor object, which is positioned before the first entry, or null
-
* @see Cursor
-
*/
-
public final Cursor query(Uri uri, String[] projection,
-
String selection, String[] selectionArgs, String sortOrder) {
-
return query(uri, projection, selection, selectionArgs, sortOrder, null);
-
}
4、以下就是获取媒体音乐库的信息了,相信大家应该都能看懂的,在这里就不详细说了。
-
public static void readDataFromSD(List<MusicInfo> songList, Context context) {
-
Cursor cursor = context.getContentResolver().query(
-
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
-
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
-
-
if (cursor != null)
-
if (cursor.moveToFirst()) {
-
do {
-
if (cursor.getString(
-
cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA))
-
.endsWith("MP3")
-
|| cursor
-
.getString(
-
cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA))
-
.endsWith("mp3")) {
-
MusicInfo songInfo = new MusicInfo();
-
songInfo.setmID(cursor.getInt(cursor
-
.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)));
-
-
if (cursor
-
.getString(cursor
-
.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)) != null) {
-
songInfo.setmDuration(cursor.getInt(cursor
-
.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
-
}
-
-
if (cursor
-
.getString(cursor
-
.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)) != null) {
-
float temp = cursor
-
.getInt(cursor
-
.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)) / 1024f / 1024f;
-
String songSize = String.format("%.2f", temp);
-
songInfo.setmSize(songSize + "M");
-
}
-
String path = cursor
-
.getString(cursor
-
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
-
songInfo.setmData(path);
-
-
String artist = cursor
-
.getString(cursor
-
.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
-
songInfo.setmName(getName(path));
-
songInfo.setmArtist(toGb2312(artist));
-
songList.add(songInfo);
-
}
-
}while (cursor.moveToNext());
-
}
-
if (cursor != null)
-
cursor.close();
-
}
5、同理:获取媒体库中的音乐就是用这种方式获取的。
-
ArrayList cameraList = new ArrayList();
-
public void getColumnData() {
-
String[] projection2 = { "_id", "_data" };
-
Uri uri2 = MediaStore.Images.Media.getContentUri("external");
-
Cursor c2 = Media.query(getContentResolver(), uri2, projection2, null,
-
MediaStore.Images.Media.DATE_ADDED);
-
-
if (c2 != null) {
-
if (c2.moveToFirst()) {
-
do {
-
String image_path = c2.getString(c2
-
.getColumnIndexOrThrow("_data"));
-
PicInfo pic = new PicInfo();
-
pic.path = image_path;
-
cameraList.add(pic);
-
-
} while (c2.moveToNext());
-
}
-
c2.close();
-
}
-
}
总结:
这个获取媒体图片、音乐的方式和联系人一样,所以不管以后类似这样的东西,我相信都能很好的解决了。
做开发的,最重要的是举一反三,我们不可能什么都记住,API里那么多类,那么多方法,所以要多去看看源码,如果还没有下载源码的就去下载,
下载了就自己导进去。有了源码就能够更为方便的知道里面的机制了。
PS: 这次就讲这么多了,至于视频资源的获取,我特意留在了下次,为什么不一次写完呢,当然是有我的原因的,具体的请看下回分析。
如果大家有什么好的建议欢迎提出,很高兴和大家分享。O(∩_∩)O哈哈~
阅读(4309) | 评论(1) | 转发(0) |