Use Meidastore.audio to get audio files from your phone

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.