Android中使用ContentProvider管理系統(tǒng)資源的實(shí)例
ContentProvider管理聯(lián)系人的實(shí)例:
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(); // 創(chuàng)建一個(gè)空的ContentValue ContentValues values = new ContentValues(); // 向RawContacts.CONTNT_URI執(zhí)行一個(gè)空值插入 // 目的是獲取系統(tǒng)返回的rawContactId Uri rawContactsUri = getContentResolver().insert( RawContacts.CONTENT_URI, values); long rawContactId = ContentUris.parseId(rawContactsUri); values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); // 設(shè)置內(nèi)容類型 values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); // 設(shè)置聯(lián)系人名字 values.put(StructuredName.GIVEN_NAME, name); // 向聯(lián)系人Uri添加聯(lián)系人名字 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); // 設(shè)置聯(lián)系人的電話 values.put(Phone.NUMBER, phone); // 設(shè)置電話類型 values.put(Phone.TYPE, Phone.TYPE_MOBILE); // 向聯(lián)系人電話Uri添加電話號(hào)碼 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); // 設(shè)置聯(lián)系人的email地址 values.put(Email.DATA, email); // 設(shè)置email的類型 values.put(Email.TYPE, Email.TYPE_WORK); getContentResolver().insert( android.provider.ContactsContract.Data.CONTENT_URI, values); Toast.makeText(MainActivity.this, "添加聯(lián)系人信息成功", Toast.LENGTH_LONG) .show(); } } class LookPresonClick implements OnClickListener { @Override public void onClick(View v) { // 定義兩個(gè)List來(lái)封裝系統(tǒng)聯(lián)系人信息,指定聯(lián)系人的電話,email等詳情 final ArrayList<String> names = new ArrayList<String>(); final ArrayList<ArrayList<String>> details = new ArrayList<ArrayList<String>>(); // 使用ContentResolver查找聯(lián)系人數(shù)據(jù) Cursor cursor = getContentResolver().query( ContactsContract.Contacts.CONTENT_URI, null, null, null, null); // 遍歷結(jié)果 獲取系統(tǒng)所有聯(lián)系人信息 while (cursor.moveToNext()) { // 獲取聯(lián)系人ID String contactid = cursor.getString(cursor .getColumnIndex(ContactsContract.Contacts._ID)); // 獲取聯(lián)系人的名字 String name = cursor .getString(cursor .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); names.add(name); // 使用ContentResolver查找聯(lián)系人的電話號(hào)碼 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>(); // 遍歷查詢結(jié)果,獲取該聯(lián)系人的多個(gè)電話 while (phones.moveToNext()) { // 獲取查詢的結(jié)果中的電話號(hào)碼列 String phoneNumber = phones .getString(phones .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); detail.add("電話號(hào)碼是:" + phoneNumber); } phones.close(); // 使用ContentResolver查找聯(lián)系人的E-mail地址 Cursor emails = getContentResolver().query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " =?", new String[] { contactid }, null); // 遍歷查詢結(jié)果,獲取該聯(lián)系人的多個(gè)email地址 while (emails.moveToNext()) { // 獲取查詢的結(jié)果中email地址中列的數(shù)據(jù) 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); // 創(chuàng)建一個(gè)ExpandableListAdapter對(duì)象 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); // 使用對(duì)話框來(lái)顯示查詢結(jié)果 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="輸入聯(lián)系人姓名"/> <EditText android:id="@+id/ed2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="輸入聯(lián)系人電話"/> <EditText android:id="@+id/ed3" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="輸入聯(lián)系人email"/> <Button android:id="@+id/bt2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加聯(lián)系人信息"/> <Button android:id="@+id/bt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查看聯(lián)系人" /> </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管理多媒體內(nèi)容
Android為多媒體提供的Uri:
1、MediaStore.Audio.Mdia.EXTERNAL_CONTENT_URI:存儲(chǔ)在外部設(shè)備上的音頻文件
2、MediaStore.Audio.Mdia.INTERNAL_CONTENT_URI:存儲(chǔ)在手機(jī)內(nèi)部上的音頻文件
3、MediaStore.Images.Mdia.EXTERNAL_CONTENT_URI:存儲(chǔ)在外部設(shè)備上的圖片文件
4、MediaStore.Images.Mdia.INTERNAL_CONTENT_URI:存儲(chǔ)在內(nèi)部設(shè)備上的圖片文件
5、MediaStore.Video.Mdia.EXTERNAL_CONTENT_URI:存儲(chǔ)在外部設(shè)備上的音頻文件
6、MediaStore.Video.Mdia.INTERNAL_CONTENT_URI:存儲(chǔ)在內(nèi)部設(shè)備上的音頻文件
實(shí)例:
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集合里原有的數(shù)據(jù) names.clear(); descs.clear(); filenames.clear(); // 通過(guò)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)); // 獲取圖片的詳細(xì)信息、 String desc = curos.getString(curos .getColumnIndex(Media.DESCRIPTION)); // 將圖片名保存的位置數(shù)據(jù) 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)); } // 創(chuàng)建一個(gè)List集合的元素是map List<Map<String, Object>> listitems = new ArrayList<Map<String, Object>>(); // 將names、descs兩個(gè)集合對(duì)象的數(shù)據(jù)轉(zhuǎn)換到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); // 設(shè)置image顯示指定的圖片 image1.setImageBitmap(BitmapFactory.decodeFile(filenames .get(arg2))); // 使用對(duì)話框顯示用戶單擊的圖片 new AlertDialog.Builder(MainActivity.this).setView(view) .setPositiveButton("確定", null).show(); } }); bt2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 創(chuàng)建ContentValues對(duì)象,準(zhǔn)備插入數(shù)據(jù) ContentValues values = new ContentValues(); values.put(Media.DISPLAY_NAME, "jinta"); values.put(Media.DESCRIPTION, "金塔"); values.put(Media.MIME_TYPE, "image/jpeg"); // 插入數(shù)據(jù)對(duì)應(yīng)的Uri Uri uri = getContentResolver().insert( Media.EXTERNAL_CONTENT_URI, values); // 加載應(yīng)用程序下的jinta圖片 Bitmap bitmap = BitmapFactory.decodeResource( MainActivity.this.getResources(), R.drawable.jinta); OutputStream os = null; try { // 獲取剛插入的數(shù)據(jù)的Uri對(duì)應(yīng)的輸出流 os = getContentResolver().openOutputStream(uri); // 將bitmap圖片保存到Uri對(duì)應(yīng)的數(shù)據(jù)節(jié)點(diǎn)中 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系統(tǒng)的基本體系結(jié)構(gòu)與內(nèi)存管理優(yōu)化
- Android開(kāi)發(fā)之媒體播放工具類完整示例
- Android開(kāi)發(fā)之彈出軟鍵盤工具類簡(jiǎn)單示例
- Android開(kāi)發(fā)之EditText框輸入清理工具類示例
- Android開(kāi)發(fā)之Activity管理工具類完整示例
- Android編程錄音工具類RecorderUtil定義與用法示例
- Android開(kāi)發(fā)中解析xml文件XmlUtils工具類與用法示例
- Android圖片處理工具類BitmapUtils
- Android封裝的http請(qǐng)求實(shí)用工具類
- 19個(gè)Android常用工具類匯總
- Android開(kāi)發(fā)之超實(shí)用的系統(tǒng)管理工具類【SD卡,網(wǎng)絡(luò),uri,屏幕,網(wǎng)絡(luò),軟鍵盤,文本,進(jìn)程等】
相關(guān)文章
Android根據(jù)電話號(hào)碼獲得聯(lián)系人頭像實(shí)例代碼
這篇文章主要介紹了Android根據(jù)電話號(hào)碼獲得聯(lián)系人頭像實(shí)例代碼,是Android程序開(kāi)發(fā)中非常重要的技巧,需要的朋友可以參考下2014-09-09android開(kāi)發(fā)基礎(chǔ)教程—SharedPreferences讀寫
本文介紹SharedPreferences的讀與寫的實(shí)現(xiàn)思路,感興趣的朋友可以了解下2013-01-01在Android Studio中設(shè)置Button透明度的方法詳解
本文將介紹在Android Studio中如何設(shè)置Button的透明度,首先,我們將展示實(shí)現(xiàn)該功能的整個(gè)流程,并使用表格列出每個(gè)步驟,然后,我們將詳細(xì)說(shuō)明每個(gè)步驟需要做什么,并提供相應(yīng)的代碼和注釋,需要的朋友可以參考下2023-09-09Android使用自定義控件HorizontalScrollView打造史上最簡(jiǎn)單的側(cè)滑菜單
側(cè)滑菜單一般都會(huì)自定義ViewGroup,然后隱藏菜單欄,當(dāng)手指滑動(dòng)時(shí),通過(guò)Scroller或者不斷的改變leftMargin等實(shí)現(xiàn);多少都有點(diǎn)復(fù)雜,完成以后還需要對(duì)滑動(dòng)沖突等進(jìn)行處理,今天給大家?guī)?lái)一個(gè)簡(jiǎn)單的實(shí)現(xiàn),史上最簡(jiǎn)單有點(diǎn)夸張,但是的確是我目前遇到過(guò)的最簡(jiǎn)單的一種實(shí)現(xiàn)2016-02-02android自定義開(kāi)關(guān)控件-SlideSwitch的實(shí)例
本篇文章主要介紹了android自定義開(kāi)關(guān)控件-SlideSwitch的實(shí)例,實(shí)現(xiàn)了手機(jī)控件開(kāi)關(guān)的功能,感興趣的小伙伴們可以參考一下。2016-11-11Android 自定義按鈕點(diǎn)擊事件和長(zhǎng)按事件對(duì)比
這篇文章主要介紹了 Android 自定義按鈕點(diǎn)擊事件和長(zhǎng)按事件對(duì)比的相關(guān)資料,需要的朋友可以參考下2017-04-04Android利用代碼控制設(shè)備上其他音樂(lè)播放器的方法
這篇文章主要給大家介紹了關(guān)于Android利用代碼如何控制設(shè)備上其他音樂(lè)播放器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06Android實(shí)現(xiàn)Camera2預(yù)覽和拍照效果
這篇文章主要為大家詳細(xì)介紹了Android開(kāi)發(fā)之一個(gè)類實(shí)現(xiàn)Camera2預(yù)覽和拍照效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10MaterialApp?Flutter?應(yīng)用全局配置與主題管理詳解
這篇文章主要為大家介紹了MaterialApp?Flutter?應(yīng)用全局配置與主題管理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03