Mediastore is the Android system comes with the multimedia system database, he is refreshed at each boot, you can use the cursor this class to access and modify the database, modified after the need for broadcast forced refresh.
Use the cursor to obtain an object instance of Contentresolver through the context.
Cursor Cursor1 = Context.getcontentresolver (). Query (MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null, null,null,null);
View Code
The context in the code is both the active object that invokes the function, and the first class that encapsulates the music information
Public classMusic {PrivateString filename; PrivateString title; Private intduring; PrivateString singer; PrivateString type; PrivateString size; PrivateString FileUrl; PublicMusic (String filename,string title,intduring,string singer,string type,string size,string fileUrl) { This. FileName =filename; This. title =title; This. during =during; This. Singer =singer; This. Type =type; This. Size =size; This. FILEURL =FileUrl; } PublicString toString () {return "song[filename="+filename+", title="+title+", during="+during+", singer="+singer+", type="+type+", size="+size+", Fileurl="+fileurl+"]"; } PublicString GetFileName () {returnfilename; } Public voidsetfilename (String filename) { This. FileName =filename; } PublicString GetTitle () {returntitle; } Public voidSettitle (String title) { This. title =title; } Public intgetduring () {returnduring; } Public voidSetduring (intduring) { This. during =during; } PublicString Getsinger () {returnsinger; } Public voidSetsinger (String singer) { This. Singer =singer; } PublicString GetType () {returntype; } Public voidsetType (String type) { This. Type =type; } PublicString GetSize () {returnsize; } Public voidsetSize (String size) { This. Size =size; } PublicString Getfileurl () {returnFileUrl; } Public voidSetfileurl (String fileUrl) { This. FILEURL =FileUrl; } }
Here is the help class for manipulating the database with the cursor.
Public classmusichelper{ Public Staticlist<music> songs =NewArraylist<music>(); Public StaticList<music>getallsongs (Context context) {Cursor Cursor1= Context.getcontentresolver (). Query (MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,NULL,NULL,NULL,NULL); Getcorrsongs (Cursor1); Cursor Cursor2= Context.getcontentresolver (). Query (MediaStore.Audio.Media.INTERNAL_CONTENT_URI,NULL,NULL,NULL,NULL); Getcorrsongs (CURSOR2); returnsongs; } Public Static voidgetcorrsongs (cursor cursor) {Music song=NULL; if(Cursor! =NULL){ while(Cursor.movetonext ()) {String filename=cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.DISPLAY_NAME)); String title=cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.TITLE)); intDuring =Cursor.getint (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.DURATION)); String singer=cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.ARTIST)); String type=cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.MIME_TYPE)); String size=cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.SIZE)); String Date=cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.DATA)); Song=NewMusic (filename,title,during,singer,type,size,date); Songs.add (song); } cursor.close (); } }}
The content of the parameter is passed in with the activity's this reference.
Allsongs = musichelper.getallsongs (this);
Use Meidastore.audio to get audio files from your phone