ContentProvider An instance of managing a contact:
Package com.android.xiong.getsystemcontentprovidertest;
Import java.util.ArrayList;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.ContentUris;
Import android.content.ContentValues;
Import Android.database.Cursor;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.provider.ContactsContract;
Import Android.provider.ContactsContract.CommonDataKinds.Email;
Import Android.provider.ContactsContract.CommonDataKinds.Phone;
Import Android.provider.ContactsContract.CommonDataKinds.StructuredName;
Import Android.provider.ContactsContract.Data;
Import android.provider.ContactsContract.RawContacts;
Import android.view.Gravity;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.ViewGroup;
Import Android.widget.AbsListView;
Import Android.widget.AbsListView.LayoutParams;
Import Android.widget.BaseExpandableListAdapter;
Import Android.widget.Button; Import andRoid.widget.EditText;
Import Android.widget.ExpandableListAdapter;
Import Android.widget.ExpandableListView;
Import Android.widget.TextView;
Import Android.widget.Toast;
public class Mainactivity extends activity {private Button bt1, BT2;
Private Expandablelistview EXP1;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
BT1 = (Button) Findviewbyid (R.ID.BT1);
Bt1.setonclicklistener (New Lookpresonclick ());
BT2 = (Button) Findviewbyid (R.ID.BT2);
Bt2.setonclicklistener (New Addpersonclick ()); Class Addpersonclick implements Onclicklistener {@Override public void OnClick (View v) {//Get process
The Mulberry text box in the order interface String name = ((edittext) Findviewbyid (r.id.ed1)). GetText (). toString ();
String phone = ((edittext) Findviewbyid (R.ID.ED2)). GetText (). toString (); String email = ((edittext) FindviewbyId (R.ID.ED3)). GetText (). toString ();
Create an empty contentvalue contentvalues values = new Contentvalues (); Executes a null value insert///purpose to Rawcontacts.contnt_uri the Rawcontactid Uri Rawcontactsuri = Getcontentresolver () returned by the system. Inser
T (Rawcontacts.content_uri, values);
Long Rawcontactid = Contenturis.parseid (Rawcontactsuri);
Values.clear ();
Values.put (data.raw_contact_id, Rawcontactid);
Sets the content type Values.put (Data.mimetype, Structuredname.content_item_type);
Set the contact name Values.put (structuredname.given_name, name); Add a contact name to the contact URI getcontentresolver (). Insert (Android.provider.ContactsContract.Data.CONTENT_URI, value
s);
Values.clear ();
Values.put (data.raw_contact_id, Rawcontactid);
Values.put (Data.mimetype, Phone.content_item_type);
Set up a contact's phone values.put (phone.number, phone);
Set Phone type values.put (Phone.type, phone.type_mobile); Add a phone number to the contact phone URI getcontentresolver (). Insert (Android.provider.ContactsContract.Data.CONTENT_URI, V
Alues);
Values.clear ();
Values.put (data.raw_contact_id, Rawcontactid);
Values.put (Data.mimetype, Email.content_item_type);
Set up a contact's email address values.put (email.data, email);
Set the type of Email values.put (Email.type, email.type_work);
Getcontentresolver (). Insert (Android.provider.ContactsContract.Data.CONTENT_URI, values);
Toast.maketext (Mainactivity.this, "Add contact information successful", Toast.length_long). Show ();
Class Lookpresonclick implements Onclicklistener {@Override public void OnClick (View v) {
Define two list to encapsulate system contact information, specify contact person's phone number, email details final arraylist<string> names = new arraylist<string> ();
Final arraylist<arraylist<string>> details = new arraylist<arraylist<string>> (); Use Contentresolver to find contact data CUrsor cursor = getcontentresolver (). query (ContactsContract.Contacts.CONTENT_URI, NULL, NULL, NULL,
NULL); Traversal results get all contact information for the system while (Cursor.movetonext ()) {//Get contact ID String ContactID = cursor.getstring (
Cursor. Getcolumnindex (contactscontract.contacts._id)); Gets the name of the contact String name = cursor. getString (cursor. Getcolumnindex contactscontract .
Contacts.display_name));
Names.add (name); Use Contentresolver to find the contact's phone number Cursor phones = getcontentresolver (). Query (Contactscontract.commonda
TaKinds.Phone.CONTENT_URI, NULL, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ "=?", new string[] {ContactID}, NULL);
arraylist<string> detail = new arraylist<string> (); Iterate through the query results, get the contact's multiple phone while (Phones.movetonext ()) {//Get the Phone number column String in the query's results PhoneNumber = phones. GetString (phones. Getcolumnindex (Contactscontract.commondatakinds .
Phone.number));
Detail.add ("Telephone number is:" + phonenumber);
} phones.close (); Use Contentresolver to find the e-mail address of a contact Cursor emails = getcontentresolver (). Query (Contactscontract.comm
OnDataKinds.Email.CONTENT_URI, NULL, ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ "=?", new string[] {ContactID}, NULL); Traverse query results, get multiple email addresses for this contact while (Emails.movetonext ()) {//Get the results of the query the data String EMA in the email address iladdress = emails. getString (emails. Getcolumnindex (ContactsContract.CommonDataKinds.Em Ail.
DATA));
Detail.add ("Email is:" + emailaddress);
} emails.close ();
Details.add (detail);
} cursor.close (); Load Result.xml Interface Layout representative View Resultdialog= Getlayoutinflater (). Inflate (R.layout.result, NULL);
EXP1 = (Expandablelistview) Resultdialog.findviewbyid (R.ID.EXP1); Create a Expandablelistadapter object Expandablelistadapter adapter = new Baseexpandablelistadapter () {@Overri De public boolean ischildselectable (int groupposition, int childposition) {//TODO auto-
Generated method stub return true; @Override public boolean hasstableids () {//TODO auto-generated method stub R
Eturn true; @Override public view Getgroupview (int groupposition, Boolean isexpanded, view Convertv
Iew, ViewGroup parent) {TextView text = Gettextveiw ();
Text.settext (Getgroup (groupposition). toString ());
return text;
@Override public long getgroupid (int groupposition) {//TODO auto-generated method stub return groupposition; @Override public int GetGroupCount () {//TODO auto-generated method stub Retu
RN Names.size ();
@Override public Object getgroup (int groupposition) {//TODO auto-generated method stub
Return Names.get (groupposition); @Override public int Getchildrencount (int groupposition) {//TODO auto-generated method
Stub return Details.get (Groupposition). Size (); Private TextView Gettextveiw () {abslistview.layoutparams LP = new Layoutparams (Vi
EwGroup.LayoutParams.MATCH_PARENT, 64);
TextView TextView = new TextView (mainactivity.this);
TEXTVIEW.SETLAYOUTPARAMS (LP); Textview.setgravity (gravity.center_vertical |
Gravity.left);
Textview.setpadding (36, 0, 0, 0);
Textview.settextsize (20);
return TextView; @Override public View getchildview (int groupposition, int childposition, Boolean is
LastChild, View Convertview, ViewGroup parent) {TextView TextView = Gettextveiw ();
Textview.settext (Getchild (Groupposition, childposition). toString ());
return TextView; @Override public long Getchildid (int groupposition, int childposition) {//TODO Auto-gen
erated method stub return childposition; @Override public Object getchild (int groupposition, int childposition) {return DETAILS.G
ET (groupposition). get (Childposition);
}
};
Exp1.setadapter (adapter); Use the dialog box to display query results new Alertdialog.builder (mainactivity.this). Setview (Resultdialog). Setpositivebutton (ok
, null). Show (); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu;This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
}
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien" tation= "Vertical" tools:context= ". Mainactivity "> <edittext android:id=" @+id/ed1 "android:layout_width=" Match_parent "Android:l" ayout_height= "Wrap_content" android:hint= "Enter contact name"/> <edittext android:id= "@+id/ed2" Androi D:layout_width= "Match_parent" android:layout_height= "wrap_content" android:hint= "Enter contact phone"/> <Edit
Text android:id= "@+id/ed3" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:hint= "Enter contact person email"/> <button android:id= @+id/bt2 "android:layout_width=" Match_par Ent "android:layout_height=" wrap_content "android:text=" Add contact information "/> <button android:id=" @+id/bt 1 "ANdroid:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "View Contacts"/> </Lin
Earlayout>
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" match_parent "
android:layout_height=" match_parent "
android:o" rientation= "vertical" >
<expandablelistview
android:id= "@+id/exp1"
Match_parent "
android:layout_height=" wrap_content ">
</ExpandableListView>
</ Linearlayout>
Managing multimedia content using ContentProvider
Android provides a URI for multimedia:
1. MediaStore.Audio.Mdia.EXTERNAL_CONTENT_URI: Audio files stored on external devices
2, MediaStore.Audio.Mdia.INTERNAL_CONTENT_URI: stored in the phone's internal audio files
3, MediaStore.Images.Mdia.EXTERNAL_CONTENT_URI: The image file stored on the external device
4. MediaStore.Images.Mdia.INTERNAL_CONTENT_URI: Picture files stored on internal devices
5. MediaStore.Video.Mdia.EXTERNAL_CONTENT_URI: Audio files stored on external devices
6. MediaStore.Video.Mdia.INTERNAL_CONTENT_URI: Audio files stored on internal devices
Instance:
Package com.example.mediaprovidertest;
Import java.io.IOException;
Import Java.io.OutputStream;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import android.content.ContentValues;
Import Android.database.Cursor;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.provider.MediaStore.Images.Media;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.Button;
Import Android.widget.ImageView;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;
Import Android.widget.TextView;
public class Mainactivity extends activity {private Button bt1, BT2;
Private ListView List1; Arraylist<string> names = new arraylist<string> ();
arraylist<string> Descs = new arraylist<string> ();
arraylist<string> filenames = new arraylist<string> ();
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
BT1 = (Button) Findviewbyid (R.ID.BT1);
BT2 = (Button) Findviewbyid (R.ID.BT2);
List1 = (ListView) Findviewbyid (r.id.list); Bt1.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//empty names
, the original data names.clear () in the DESC and filename collection;
Descs.clear ();
Filenames.clear (); Query all picture information through contentresolver Cursor Curos = Getcontentresolver (). Query (Media.external_content_uri,
NULL, NULL, NULL, NULL); while (Curos.movetonext ()) {//Get the name of the picture displayed String name = Curos.getstring (Curos. Getcol Umnindex(Media.display_name));
Gets the details of the picture, String desc = curos.getstring (curos. Getcolumnindex (media.description));
Place the name of the picture saved byte[] data = Curos.getblob (Curos. Getcolumnindex (Media.data));
Add the picture name to the Names collection Names.add (name);
Adds a picture description to the Desc collection descs.add (DESC);
Add the picture save path to the Filenames collection Filenames.add (new String (data, 0, data.length-1)); The element that creates a List collection is the Map list<map<string, object>> listitems = new arraylist<map<string,
Object>> (); Converts data from names, Descs two collection objects to the Map collection for (int i = 0; i < names.size (); i++) {map<string, object>
ListItem = new hashmap<string, object> ();
Listitem.put ("Name", Names.get (i));
Listitem.put ("desc", Descs.get (i));
Listitems.add (ListItem); } simpleadapter simple = new Simpleadapter(Mainactivity.this, ListItems, R.layout.items, new string[] {"name", "Desc"}, new int[] {
R.id.txt1, r.id.txt2});
List1.setadapter (simple);
}
}); List1.setonitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (adapterview< ?> arg0, view arg1, int arg2, long arg3) {//Load View.xml Interface Layout representative view views = GETLAYOUTINFL
Ater (). Inflate (R.layout.view, NULL);
Gets the ImageView component ImageView Image1 = (imageview) View.findviewbyid (r.id.image1) in Viewdialog;
Set image to display the specified picture Image1.setimagebitmap (bitmapfactory.decodefile (filenames. Get (ARG2))); Use the dialog box to display the picture new Alertdialog.builder (mainactivity.this). Setview (view) that the user clicked. Setpositivebutton ("OK",
NULL). Show ();
}
}); Bt2.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//Create Contentvalues object, ready to insert data contentvalues values = new Contentvalues ();
Values.put (Media.display_name, "Jinta");
Values.put (media.description, "urn");
Values.put (Media.mime_type, "image/jpeg");
The URI Uri URI of the inserted data = Getcontentresolver (). Insert (Media.external_content_uri, values); Load the Jinta picture under the application Bitmap Bitmap = Bitmapfactory.decoderesource (MainActivity.this.getResources (), R
. Drawable.jinta);
OutputStream OS = null;
try {//get the output stream OS = Getcontentresolver (). Openoutputstream (URI) for the URI of the data just inserted;
Save the bitmap picture to the corresponding data node in the URI bitmap.compress (Bitmap.CompressFormat.JPEG, OS);
Os.close ();
catch (IOException io) {io.printstacktrace ();
}
}
}); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio N Bar if itis present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
}
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
android:orientation= "vertical"
tools:context= ". Mainactivity ">
<button
android:id=" @+id/bt1 "
android:layout_width=" Match_parent "
Android : layout_height= "wrap_content"
android:text= "view picture"/>
<button android:id=
"@+id/bt2"
Android:layout_width= "Match_parent"
android:layout_height= "wrap_content"
android:text= "add Picture"/>
<listview
android:id= "@+id/list"
android:layout_width= "match_parent"
android:layout_ height= "Wrap_content" ></ListView>
</LinearLayout>
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout "xmlns:android=" Schemas.android.com/apk/res/android "android:layout_width=" match_parent "android:layout_height=" Match_parent "and
roid:orientation= "vertical" > <textview android:id= "@+id/txt1" android:layout_width= "Match_parent" android:layout_height= "Wrap_content"/> <textview android:id= "@+id/txt2" android:layout_width= Parent "android:layout_height=" wrap_content "/> </LinearLayout>
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" match_parent "
android:layout_height=" match_parent "
android:o" rientation= "vertical" >
<imageview
android:id= "@+id/image1" android:layout_width= "Match_"
Parent "
android:layout_height=" match_parent "/>
</LinearLayout>