Android also provides a ContentProvider (content provider) that can provide data to other apps, but not often, except for apps developed by the same company that can provide data to different apps. Although it is one of the four components of Android, the usage is not complicated.
Create an Activity, set up three buttons, respectively, for the Mobile album,mp3,mp4 data acquisition. Here is the use of internal class monitoring method, do not forget to register for monitoring.
PackageCom.example.administrator.jreduch09.contentprovider;
ImportAndroid.database.Cursor;
ImportAndroid.os.Bundle;
ImportAndroid.provider.MediaStore;
Importandroid.support.v7.app.AppCompatActivity;
ImportAndroid.view.View;
ImportAndroid.widget.Button;
ImportAndroid.widget.TextView;
ImportCOM.EXAMPLE.ADMINISTRATOR.JREDUCH09.R;
Public classContentactivityextendsappcompatactivity {
PrivateButtonimg,mp3,mp4;
PrivateTextViewShow;
@Override
protected voidOnCreate (Bundle savedinstancestate) {
Super. OnCreate (Savedinstancestate);
Setcontentview (r.layout.activity_content);
img= (Button) Findviewbyid (r.id.img);
mp3= (Button) Findviewbyid (r.id.mp3);
mp4= (Button) Findviewbyid (r.id.mp4);
Show= (TextView) Findviewbyid (r.id.Show);
img. Setonclicklistener (NewMylisterner ());
mp3. Setonclicklistener (NewMylisterner ());
mp4. Setonclicklistener (NewMylisterner ());
}
Public classMylisternerImplementsView.onclicklistener {
@Override
Public voidOnClick (View v) {
intid = V.getid ();
if(id = = R.id.img) {
String str[] = {MediaStore.Images.Media._id,
MediaStore.Images.Media.Display_name,
MediaStore.Images.Media.DATA};
cursor cursor = Getcontentresolver ()
. query (MediaStore.Images.Media. External_content_uri
Str
NULL,NULL,NULL);
StringBuilder SBD =NewStringBuilder ();
while(Cursor.movetonext ()) {
Sbd.append (Cursor.getstring (0) +":");
Sbd.append (Cursor.getstring (1) +":");
Sbd.append (Cursor.getstring (2) +"\ n" );
}
Show. SetText (Sbd.tostring ());
}else if(id = = R.id.mp3) {
String str[] = {MediaStore.Audio.Media._id,
MediaStore.Audio.Media.Display_name,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.SIZE,
MediaStore.Audio.Media.ARTIST, //author
MediaStore.Audio.Media.DURATION,Often
MediaStore.Audio.Media.ALBUM}; //albums
cursor cursor = getcontentresolver (). Query (
MediaStore.Audio.Media.External_content_uri,
Str
"DURATION>?",
Newstring[]{" the"},NULL);
StringBuilder SBD =NewStringBuilder ();
while(Cursor.movetonext ()) {
Sbd.append (Cursor.getstring (0) +":");
Sbd.append (Cursor.getstring (1) +":");
Sbd.append (Cursor.getstring (2) +":");
Sbd.append (Cursor.getstring (3) +":");
Sbd.append (Cursor.getstring (4) +":");
Sbd.append (Cursor.getstring (5) +":");
Sbd.append (Cursor.getstring (6) +"\ n" );
}
Show. SetText (Sbd.tostring ());
}else if(id = = R.id.mp4) {
String str[] = {MediaStore.Video.Media._id,
MediaStore.Video.Media.Display_name,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.SIZE};
cursor cursor = getcontentresolver (). Query (
MediaStore.Video.Media.External_content_uri,
Str
NULL,NULL,NULL);
StringBuilder SBD =NewStringBuilder ();
while(Cursor.movetonext ()) {
Sbd.append (Cursor.getstring (0) +":");
Sbd.append (Cursor.getstring (1) +":");
Sbd.append (Cursor.getstring (2) + ":");
Sbd.append (cursor.getstring (1) + "\ n");
}
Show . SetText (Sbd.tostring ());
}
}
}
}
One of the four components of Android: ContentProvider (content Provider)