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, values); values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, 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, 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, null, 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, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " =?", new String[] { contactid }, 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, 0, 0); textview.setTextSize(20); return textview; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 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-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, null, null, 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, 0, 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>