ContentResolver的使用

274 篇文章 4 订阅
55 篇文章 1 订阅

早期的部分代码

	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();
			}
		}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值