ContentProvider的使用

获取手机中音乐列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* 用于从数据库中查询歌曲的信息,保存在List当中
*/
public static List<Mp3Infos> getMp3Infos(Context context) {
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
List<Mp3Infos> mp3Infos = new ArrayList<Mp3Infos>();
for (int i = 0; i < cursor.getCount(); i++) {
Mp3Infos mp3Info = new Mp3Infos();
cursor.moveToNext();
long id = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media._ID)); // 音乐id
String title = cursor.getString((cursor
.getColumnIndex(MediaStore.Audio.Media.TITLE)));// 音乐标题
String artist = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST));// 艺术家
long duration = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media.DURATION));// 时长
long size = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media.SIZE)); // 文件大小
String url = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA)); // 文件路径
int isMusic = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));// 是否为音乐
if (isMusic != 0) { // 只把音乐添加到集合当中
mp3Info.setId(id);
mp3Info.setTitle(title);
mp3Info.setArtist(artist);
mp3Info.setDuration(duration);
mp3Info.setSize(size);
mp3Info.setPath(url);
mp3Infos.add(mp3Info);
}
}
return mp3Infos;
}

其中Mp3Infos如下:

1
2
3
4
5
6
7
8
9
10
11
12
public class Mp3Infos {
private long id; // 音乐id
private String title;// 音乐标题
private String artist;// 艺术家
private long duration;// 时长
private long size;// 文件大小
private String path;// 文件路径
// 省略setter和getter方法...
}

获取系统联系人

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public class ContactUtils {
private static final String[] PHONE_PROJECTION = new String[] {
Phone.DISPLAY_NAME, Phone.NUMBER, Phone.PHOTO_ID, Phone.CONTACT_ID };
private Context context;
/**
* 联系人显示名称
*/
private static final int PHONES_DISPLAYNAME_INDEX = 0;
/**
* 电话号码
*/
private static final int PHONES_NUMBER_INDEX = 1;
/**
* 头像ID
*/
private static final int PHONES_PHOTO_ID_INDEX = 2;
/**
* 联系人的ID
*/
private static final int PHONES_CONTACT_ID_INDEX = 3;
public ContactUtils(Context context) {
this.context = context;
}
/**
* 获得手机通讯录联系人
*
* @param context
*/
public List<Contact> getPhoneContact() {
List<Contact> mContactsList = new ArrayList<Contact>();
ContentResolver resolver = context.getContentResolver();
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,
PHONE_PROJECTION, null, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
// 得到手机号码
String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
// 当手机号码为空时,跳过当前循环
if (TextUtils.isEmpty(phoneNumber)) {
continue;
}
//得到联系人名称
String contactName = phoneCursor
.getString(PHONES_DISPLAYNAME_INDEX);
//得到联系人ID
Long contactId = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);
//获得联系人头像ID
Long photoId = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);
//得到联系人头像Bitmap
Bitmap contactPhoto = null;
//如果photoId 大于0,表示联系人有头像,如果没有,给此人设置默认头像
if (photoId > 0) {
Uri uri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, contactId);
InputStream is = ContactsContract.Contacts
.openContactPhotoInputStream(resolver, uri);
contactPhoto = BitmapFactory.decodeStream(is);
}else {
contactPhoto = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_head);
}
Contact contact = new Contact(contactName, phoneNumber, contactPhoto);
mContactsList.add(contact);
}
phoneCursor.close();
}
return mContactsList;
}
}

添加用户权限:

1
<uses-permission android:name="android.permission.READ_CONTACTS"/>