Android One of the four components: ContentProvider (content Provider)

Source: Internet
Author: User

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)

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.