早期的部分代码
public static ArrayList<PhoneContact> getAllContacts(Context context) {
ContentResolver resolver = context.getContentResolver();
// 1. 查询raw_contacts表,把联系人的id取出来
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri datauri = Uri.parse("content://com.android.contacts/data");
ArrayList<PhoneContact> infos = new ArrayList<PhoneContact>();
Cursor cursor = resolver.query(uri, new String[] { "contact_id" },
null, null, null);
while (cursor.moveToNext()) {
String id = cursor.getString(0);
if (id != null) {
System.out.println("联系人id:" + id);
PhoneContact info = new PhoneContact();
info.setId(id);
// 2. 根据联系人的id,查询data表,把这个id的数据取出来
// 系统api 查询data表的时候 不是真正的查询data表 而是查询的data表的视图
Cursor dataCursor = resolver.query(datauri, new String[] {
"data1", "mimetype" }, "raw_contact_id=?",
new String[] { id }, null);
while (dataCursor.moveToNext()) {
String data1 = dataCursor.getString(0);
String mimetype = dataCursor.getString(1);
if ("vnd.android.cursor.item/name".equals(mimetype)) {
System.out.println("姓名=" + data1);
info.setName(data1);
} else if ("vnd.android.cursor.item/email_v2"
.equals(mimetype)) {
System.out.println("邮箱=" + data1);
info.setEmail(data1);
} else if ("vnd.android.cursor.item/phone_v2"
.equals(mimetype)) {
System.out.println("电话=" + data1);
info.setPhone(data1);
} else if ("vnd.android.cursor.item/im".equals(mimetype)) {
System.out.println("QQ=" + data1);
info.setQq(data1);
}else if("vnd.android.cursor.item/sip_address".equals(mimetype)){
System.out.println("地址=" + data1);
info.setAddress(data1);
}
}
//判断name是否为空
if(info.getName()==null){
}else{
infos.add(info);
}
dataCursor.close();
}
}
cursor.close();
return infos;
}
public static void initMusic(Context context,Handler handler) {
songLists.clear();
ContentResolver resolver = context.getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media._ID };
Cursor c = resolver.query(uri, projection, null, null, null);
try {
while (c.moveToNext()) {
String name = c.getString(c.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = c.getString(c.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String path = c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA));
String duration = c.getString(c.getColumnIndex(MediaStore.Audio.Media.DURATION));
String id = c.getString(c.getColumnIndex(MediaStore.Audio.Media._ID));
Music music = new Music(name, artist, path, duration, id);
songLists.add(music);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (c != null) {
c.close();
}
}
handler.sendEmptyMessage(1);
}
public static void initVideo(Context context, Handler handler) {
mhandler = handler;
VideoLists.clear();
mContext=context;
ContentResolver resolver = context.getContentResolver();
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.ARTIST, MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DURATION, MediaStore.Video.Media._ID };
Cursor c = resolver.query(uri, projection, null, null, null);
try {
while (c.moveToNext()) {
String name = c.getString(c
.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = c.getString(c
.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String path = c.getString(c
.getColumnIndex(MediaStore.Audio.Media.DATA));
String duration = c.getString(c
.getColumnIndex(MediaStore.Audio.Media.DURATION));
String id = c.getString(c
.getColumnIndex(MediaStore.Audio.Media._ID));
Video video = new Video(name, artist, path, duration, id);
System.out.println("aaaa--111111--" + video.toString());
VideoLists.add(video);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (c != null) {
c.close();
}
}