ContentResolver如何获取本地媒体文件
一 本地媒体文件的URI:
Android系统的ContentProvider提供的媒体文件信息都在"android.provider.MediaStore"类中
android.provider.MediaStore:该类封装了Audio,Files, Images, Video等媒体文件的URI信息和数据表结构信息
"红色"----表示媒体文件的类别 "黄色"----表示对应媒体文件操作的数据表
所有音乐文件的URI:MediaStore.Audio.Media.EXTERNAL_CONTENT_URI 实际值为:content://media/external/audio/media
音乐文件对应专辑(Albums)URI : MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI 实际值为:content://media/external/audio/albums
二 通过ContentResolver获取所用音乐文件
1 public class LocalMusicUtil { 2 3 private static final String TAG = LocalMusicUtil.class.getSimpleName(); 4 /** 5 * 获取本地所有的音乐文件 6 */ 7 //表示操作的表 Uri.parse("content://media/external/audio/media"); 8 private static Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 9 //表示要获取的字段 10 private static String[] projection = new String[]{ 11 String.valueOf(Media._ID), 12 Media.TITLE, 13 Media.ALBUM, 14 Media.ALBUM_ID, 15 Media.ARTIST, 16 Media.DURATION, 17 Media.DATA}; 18 //表示where 子句 19 private static String selection = "mime_type in ('audio/mpeg','audio/x-ms-wma') and is_music>0 "; 20 //表示排序字段 21 private static String sortOrder = Media.DEFAULT_SORT_ORDER; 22 23 public static ArrayList<Music> getLocalMusics(){ 24 ArrayList<Music> musicList = new ArrayList<Music>(); 25 ContentResolver content = MyApp.appContext.getContentResolver(); 26 Cursor cursor = content.query(uri, projection, selection, null, sortOrder); 27 if(cursor==null){ 28 Log.v(TAG, "Music corsor==null"); 29 }else if(!cursor.moveToFirst()){ 30 Log.v(TAG, "Music corsor.moveToForst() is false"); 31 }else{ 32 int idCol = cursor.getColumnIndex(Media._ID); 33 int titleCol = cursor.getColumnIndex(Media.TITLE); 34 int albumCol = cursor.getColumnIndex(Media.ALBUM); 35 int albumIdCol = cursor.getColumnIndex(Media.ALBUM_ID); 36 int artistCol = cursor.getColumnIndex(Media.ARTIST); 37 int durationCol = cursor.getColumnIndex(Media.DURATION); 38 int dataCol = cursor.getColumnIndex(Media.DATA); 39 do{ 40 int id = cursor.getInt(idCol); 41 String title = cursor.getString(titleCol); 42 String album = cursor.getString(albumCol); 43 String albumImage = getAlbumImage(cursor.getString(albumIdCol)); 44 String artist = cursor.getString(artistCol); 45 int duration = cursor.getInt(durationCol); 46 String data = cursor.getString(dataCol); 47 48 Music music = new Music(); 49 music.setId(id); 50 music.setTitle(title); 51 music.setAlbum(album); 52 music.setAlbumImage(albumImage); 53 music.setArtist(artist); 54 music.setDuration(duration); 55 music.setData(data); 56 musicList.add(music); 57 58 }while(cursor.moveToNext()); 59 } 60 cursor.close(); 61 return musicList.size()==0?null:musicList; 62 } 63 64 /** 65 * 通过专辑名id(albumId)获取专辑的图片地址 66 */ 67 68 public static String getAlbumImage(String albumId){ 69 String albumImage = ""; 70 Cursor cursor = null; 71 try { 72 cursor = MyApp.appContext.getContentResolver().query( 73 Uri.parse(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI+"/"+albumId), 74 new String[] { "album_art" }, 75 null, null, null); 76 for (cursor.moveToFirst(); !cursor.isAfterLast();) { 77 albumImage = cursor.getString(0); 78 break; 79 } 80 } catch (Exception e) { 81 e.printStackTrace(); 82 } finally { 83 if (null != cursor) { 84 cursor.close(); 85 } 86 } 87 return albumImage; 88 } 89 }