如何获取设备上的歌曲的专辑信息?
1.系统在SD卡挂载的时候,就扫描音频和视频文件,并把信息保存到数据库中,通过ContentProvider,对外提供数据;
2.应用程序Context,可以获取ContentResolver:ContentResolver resolver = context.getContentResolver();
3.ContentResolver,以系统为纽带,以uri为语言,让ContentResolver与ContentProvider会话;
理解:ContentResolver执行query方法,是与系统会话,系统拿着uri和数据包,去找对应的ContentProvider,ContentProvider执行任务返回结果给系统,系统返回给ContentResolver;这个好比进程间通信,通过隐式意图启动其他程序的组件;
Cursor getAlbumFromAlbumId(long albumId){
ContentResolver resolver = ctx.getContentResolver();
Cursor c = null;
//如果用户设置选择的是外存储设备,就查询外部存储设备的表;
if(DirectoryFilter.usesExternalStorage()) {
//query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder);
//专辑信息表,各列的名称;
String[] albumProjection =
{
MediaStore.Audio.Albums._ID,
MediaStore.Audio.Albums.ALBUM_KEY,
// MediaStore.Audio.Albums.ALBUM_ID,
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Albums.ARTIST,
MediaStore.Audio.Albums.ALBUM_ART,
MediaStore.Audio.Albums.LAST_YEAR
};
c = resolver.query(
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,//外部存储设备的音频专辑所在表的uri
Constants.albumProjection, //专辑映射,String[],要选择哪些列?
MediaStore.Audio.Albums._ID + " = "+albumId, //选择调节
null, //占位符对应的值;
//Constants.albumAlphabeticalSortOrder);
albumProjection;
} else {
c = resolver.query(
MediaStore.Audio.Albums.INTERNAL_CONTENT_URI,
Constants.albumProjection,
MediaStore.Audio.Albums._ID + " = "+albumId,
null,
Constants.albumAlphabeticalSortOrder);
}
return c;
// return resolver.query(
// MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
// Constants.albumProjection,
// MediaStore.Audio.Albums._ID + " = "+albumId,
// null,
// Constants.albumAlphabeticalSortOrder);
}
思考:我们可以通过ContentResovler去获取ContentProvider提供的数据;那么我们如何创建ContentProvider,让别人的ContentResovler调用呢?