码迷,mamicode.com
首页 > 其他好文 > 详细

ContentProvider之通过ContentResolver获取图像、视频、音频举例

时间:2017-05-04 09:44:42      阅读:662      评论:0      收藏:0      [点我收藏+]

标签:details   not   信息   http   views   定义   数据   roc   androi   

MediaStore中定义了一系列的数据表格,通过ContentResolver提供的查询接口,我们能够得到各种须要的媒体信息。通过下面两个URI能够扫描设备外部和内部的媒体文件。Android系统提供了MediaProvider,MediaStore。MediaScanner接口,通过Content Provider的方式提供给用户。

下面是一个简单的样例:(注意。项目使用AndroidAnnotation框架)

package com.example.testaa;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.TextView;
/*
 *@author: ZhengHaibo  
 *web:     http://blog.csdn.net/nuptboyzhb
 *mail:    zhb931706659@126.com
 *2014-4-15  Nanjing,njupt,China
 */
@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {

	@ViewById
	Button getImage;

	@ViewById
	Button getVideo;

	@ViewById
	Button getAudio;

	@ViewById
	TextView myTextView;

	/**
	 * 载入完View之后进行的处理
	 */
	@AfterViews
	void afterViewProcess() {

	}

	/**
	 * 获取图像列表
	 */
	@Click
	void getImage() {
		String[] projection = { MediaStore.Images.Media._ID,
				MediaStore.Images.Media.DISPLAY_NAME,
				MediaStore.Images.Media.DATA };
		String orderBy = MediaStore.Images.Media.DISPLAY_NAME;
		Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
		getContentProvider(uri,projection, orderBy);
	}

	/**
	 * 获取视频列表
	 */
	@Click
	void getVideo() {
		// TODO Auto-generated method stub
		 String []projection = { MediaStore.Video.Media._ID,  
                 MediaStore.Video.Media.DISPLAY_NAME,  
                 MediaStore.Video.Media.DATA}; 
		String orderBy = MediaStore.Video.Media.DISPLAY_NAME;
		Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
		getContentProvider(uri,projection, orderBy);
	}

	/**
	 * 获取音频列表
	 */
	@Click
	void getAudio() {
		String []projection = { MediaStore.Audio.Media._ID,  
                MediaStore.Audio.Media.DISPLAY_NAME,  
                MediaStore.Audio.Media.DATA,  
                MediaStore.Audio.Media.SIZE}; 
		String orderBy = MediaStore.Audio.Media.DISPLAY_NAME;
		Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
		getContentProvider(uri,projection, orderBy);
	}
	
	/**
	 * 获取ContentProvider
	 * @param projection
	 * @param orderBy
	 */
	public void getContentProvider(Uri uri,String[] projection, String orderBy) {
		// TODO Auto-generated method stub
		List<HashMap<String, String>> listImage = new ArrayList<HashMap<String, String>>();
		Cursor cursor = getContentResolver().query(uri, projection, null,
				null, orderBy);
		if (null == cursor) {
			return;
		}
		while (cursor.moveToNext()) {
			HashMap<String, String> map = new HashMap<String, String>();
			for(int i=0;i<projection.length;i++){
				map.put(projection[i], cursor.getString(i));
				System.out.println(projection[i]+":"+cursor.getString(i));
			}
			listImage.add(map);
		}
		myTextView.setText(listImage.toString());
	}

}

项目下载地址:http://download.csdn.net/detail/nuptboyzhb/7220227

扩展阅读:

【1】http://blog.csdn.net/gaomatrix/article/details/6216880

【2】http://developer.android.com/reference/android/content/ContentProvider.html

ContentProvider之通过ContentResolver获取图像、视频、音频举例

标签:details   not   信息   http   views   定义   数据   roc   androi   

原文地址:http://www.cnblogs.com/cxchanpin/p/6805170.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!