ContentProvIDer管理联系人的实例:
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) { // 获取程序界面中的桑文本框 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(); // 创建一个空的ContentValue ContentValues values = new ContentValues(); // 向RawContacts.CONTNT_URI执行一个空值插入 // 目的是获取系统返回的rawContactID Uri rawContactsUri = getContentResolver().insert( RawContacts.CONTENT_URI,values); long rawContactID = ContentUris.parseID(rawContactsUri); values.clear(); values.put(Data.RAW_CONTACT_ID,rawContactID); // 设置内容类型 values.put(Data.MIMETYPE,Structuredname.CONTENT_ITEM_TYPE); // 设置联系人名字 values.put(Structuredname.GIVEN_name,name); // 向联系人Uri添加联系人名字 getContentResolver().insert( androID.provIDer.ContactsContract.Data.CONTENT_URI,values); values.clear(); values.put(Data.RAW_CONTACT_ID,rawContactID); values.put(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE); // 设置联系人的电话 values.put(Phone.NUMBER,phone); // 设置电话类型 values.put(Phone.TYPE,Phone.TYPE_MOBILE); // 向联系人电话Uri添加电话号码 getContentResolver().insert( androID.provIDer.ContactsContract.Data.CONTENT_URI,Email.CONTENT_ITEM_TYPE); // 设置联系人的email地址 values.put(Email.DATA,email); // 设置email的类型 values.put(Email.TYPE,Email.TYPE_WORK); getContentResolver().insert( androID.provIDer.ContactsContract.Data.CONTENT_URI,values); Toast.makeText(MainActivity.this,"添加联系人信息成功",Toast.LENGTH_LONG) .show(); } } class LookPresonClick implements OnClickListener { @OverrIDe public voID onClick(VIEw v) { // 定义两个List来封装系统联系人信息,指定联系人的电话,email等详情 final ArrayList<String> names = new ArrayList<String>(); final ArrayList<ArrayList<String>> details = new ArrayList<ArrayList<String>>(); // 使用ContentResolver查找联系人数据 Cursor cursor = getContentResolver().query( ContactsContract.Contacts.CONTENT_URI,null,null); // 遍历结果 获取系统所有联系人信息 while (cursor.movetoNext()) { // 获取联系人ID String contactID = cursor.getString(cursor .getColumnIndex(ContactsContract.Contacts._ID)); // 获取联系人的名字 String name = cursor .getString(cursor .getColumnIndex(ContactsContract.Contacts.disPLAY_name)); names.add(name); // 使用ContentResolver查找联系人的电话号码 Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "= ?",new String[] { contactID },null); ArrayList<String> detail = new ArrayList<String>(); // 遍历查询结果,获取该联系人的多个电话 while (phones.movetoNext()) { // 获取查询的结果中的电话号码列 String phoneNumber = phones .getString(phones .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); detail.add("电话号码是:" + phoneNumber); } phones.close(); // 使用ContentResolver查找联系人的E-mail地址 Cursor emails = getContentResolver().query( ContactsContract.CommonDataKinds.Email.CONTENT_URI,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " =?",null); // 遍历查询结果,获取该联系人的多个email地址 while (emails.movetoNext()) { // 获取查询的结果中email地址中列的数据 String emailAddress = emails .getString(emails .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); detail.add("email是:" + emailAddress); } emails.close(); details.add(detail); } cursor.close(); // 加载result.xml界面布局代表的视图 VIEw resultDialog = getLayoutInflater().inflate(R.layout.result,null); exp1 = (ExpandableListVIEw) resultDialog.findVIEwByID(R.ID.exp1); // 创建一个expandablelistadapter对象 expandablelistadapter adapter = new Baseexpandablelistadapter() { @OverrIDe public boolean isChildSelectable(int groupposition,int childposition) { // Todo auto-generated method stub return true; } @OverrIDe public boolean hasStableIDs() { // Todo auto-generated method stub return true; } @OverrIDe public VIEw getGroupVIEw(int groupposition,boolean isExpanded,VIEw convertVIEw,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 return 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( VIEwGroup.LayoutParams.MATCH_PARENT,64); TextVIEw textvIEw = new TextVIEw(MainActivity.this); textvIEw.setLayoutParams(lp); textvIEw.setGravity(Gravity.CENTER_VERTICAL | Gravity.left); textvIEw.setpadding(36,0); textvIEw.setTextSize(20); return textvIEw; } @OverrIDe public VIEw getChildVIEw(int groupposition,int childposition,boolean isLastChild,VIEwGroup parent) { TextVIEw textvIEw = getTextVeiw(); textvIEw.setText(getChild(groupposition,childposition) .toString()); return textvIEw; } @OverrIDe public long getChildID(int groupposition,int childposition) { // Todo auto-generated method stub return childposition; } @OverrIDe public Object getChild(int groupposition,int childposition) { return details.get(groupposition).get(childposition); } }; exp1.setAdapter(adapter); // 使用对话框来显示查询结果 new AlertDialog.Builder(MainActivity.this).setVIEw(resultDialog) .setPositivebutton("确定",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:orIEntation="vertical" tools:context=".MainActivity" > <EditText androID:ID="@+ID/ed1" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:hint="输入联系人姓名"/> <EditText androID:ID="@+ID/ed2" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:hint="输入联系人电话"/> <EditText androID:ID="@+ID/ed3" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:hint="输入联系人email"/> <button androID:ID="@+ID/bt2" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="添加联系人信息"/> <button androID:ID="@+ID/bt1" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="查看联系人" /> </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:orIEntation="vertical" > <ExpandableListVIEw androID:ID="@+ID/exp1" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" > </ExpandableListVIEw> </linearLayout>
使用ContentProvIDer管理多媒体内容
AndroID为多媒体提供的Uri:
1、MediaStore.Audio.Mdia.EXTERNAL_CONTENT_URI:存储在外部设备上的音频文件
2、MediaStore.Audio.Mdia.INTERNAL_CONTENT_URI:存储在手机内部上的音频文件
3、MediaStore.Images.Mdia.EXTERNAL_CONTENT_URI:存储在外部设备上的图片文件
4、MediaStore.Images.Mdia.INTERNAL_CONTENT_URI:存储在内部设备上的图片文件
5、MediaStore.VIDeo.Mdia.EXTERNAL_CONTENT_URI:存储在外部设备上的音频文件
6、MediaStore.VIDeo.Mdia.INTERNAL_CONTENT_URI:存储在内部设备上的音频文件
实例:
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) { // 清空names、desc、filename集合里原有的数据 names.clear(); descs.clear(); filenames.clear(); // 通过ContentResolver查询所有图片信息 Cursor curos = getContentResolver().query( Media.EXTERNAL_CONTENT_URI,null); while (curos.movetoNext()) { // 获取图片显示的名字 String name = curos.getString(curos .getColumnIndex(Media.disPLAY_name)); // 获取图片的详细信息、 String desc = curos.getString(curos .getColumnIndex(Media.DESCRIPTION)); // 将图片名保存的位置数据 byte[] data = curos.getBlob(curos .getColumnIndex(Media.DATA)); // 将图片名添加到names集合中 names.add(name); // 将图片描述添加到desc集合中 descs.add(desc); // 将图片保存路径添加到filenames集合中 filenames.add(new String(data,data.length - 1)); } // 创建一个List集合的元素是map List<Map<String,Object>> Listitems = new ArrayList<Map<String,Object>>(); // 将names、descs两个集合对象的数据转换到map集合 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) { // 加载vIEw.xml界面布局代表视图 VIEw vIEw = getLayoutInflater().inflate(R.layout.vIEw,null); // 获取vIEwDialog中ImageVIEw组件 ImageVIEw image1 = (ImageVIEw) vIEw.findVIEwByID(R.ID.image1); // 设置image显示指定的图片 image1.setimageBitmap(BitmapFactory.decodefile(filenames .get(arg2))); // 使用对话框显示用户单击的图片 new AlertDialog.Builder(MainActivity.this).setVIEw(vIEw) .setPositivebutton("确定",null).show(); } }); bt2.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { // 创建ContentValues对象,准备插入数据 ContentValues values = new ContentValues(); values.put(Media.disPLAY_name,"jinta"); values.put(Media.DESCRIPTION,"金塔"); values.put(Media.MIME_TYPE,"image/jpeg"); // 插入数据对应的Uri Uri uri = getContentResolver().insert( Media.EXTERNAL_CONTENT_URI,values); // 加载应用程序下的jinta图片 Bitmap bitmap = BitmapFactory.decodeResource( MainActivity.this.getResources(),R.drawable.jinta); OutputStream os = null; try { // 获取刚插入的数据的Uri对应的输出流 os = getContentResolver().openOutputStream(uri); // 将bitmap图片保存到Uri对应的数据节点中 bitmap.compress(Bitmap.CompressFormat.JPEG,100,os); os.close(); } catch (IOException io) { io.printstacktrace(); } } }); } @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:orIEntation="vertical" tools:context=".MainActivity" > <button androID:ID="@+ID/bt1" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="查看图片"/> <button androID:ID="@+ID/bt2" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="添加图片"/> <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="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID: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="match_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:orIEntation="vertical" > <ImageVIEw androID:ID="@+ID/image1" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" /> </linearLayout>
总结
以上是内存溢出为你收集整理的Android中使用ContentProvider管理系统资源的实例全部内容,希望文章能够帮你解决Android中使用ContentProvider管理系统资源的实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)