/**
* mediaStore检索视频
*
* @time 下午05:15:41
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class VideoGalleyActivity extends Activity implements OnClickListener, OnItemClickListener {
private ListView listView;
private Cursor cursor;
/**
* 视频文件信息
*/
class VideoViewInfo {
String filePath;
String mimeType;
String thumbPath;
String title;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_gallery);
findView();
init();
getVideo();
}
/**
* 初始化方法
*/
private void init() {
}
/**
* 实例化控件
*/
private void findView() {
listView = (ListView) this.findViewById(R.id.listView);
listView.setOnItemClickListener(this);
}
@Override
public void onClick(View v) {
}
/**
* mediaStore检索视频
*/
private void getVideo() {
String[] mediaColumns = { MediaStore.Video.Media._ID,//
MediaStore.Video.Media.DATA,//
MediaStore.Video.Media.TITLE,//
MediaStore.Video.Media.MIME_TYPE };
String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA,//
MediaStore.Video.Thumbnails.VIDEO_ID //
};
cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);
ArrayList<VideoViewInfo> viewInfos = new ArrayList<VideoGalleyActivity.VideoViewInfo>();
if (cursor.moveToFirst()) {
do {
VideoViewInfo info = new VideoViewInfo();
// 获取缩略图
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media._ID));
String selection = MediaStore.Video.Thumbnails.VIDEO_ID + "=?";
String[] selectionArgs = new String[] { id + "" };
Cursor thumbCursor = this.managedQuery(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, thumbColumns, selection, selectionArgs, null);
if (thumbCursor.moveToFirst()) {
info.thumbPath = thumbCursor.getString(thumbCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
}
info.filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
info.title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.TITLE));
info.mimeType = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.MIME_TYPE));
viewInfos.add(info);
} while (cursor.moveToNext());
}
listView.setAdapter(new VideoGalleryAdapter(VideoGalleyActivity.this, viewInfos));
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (cursor.moveToPosition(position)) {
int fileColum = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
int mimeColum = cursor.getColumnIndex(MediaStore.Video.Media.MIME_TYPE);
String videoFilePath = cursor.getString(fileColum);
String mimeType = cursor.getString(mimeColum);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
File file = new File(videoFilePath);
intent.setDataAndType(Uri.fromFile(file), mimeType);
startActivity(intent);
}
}
/*****************************************/
/**
*
*/
class VideoGalleryAdapter extends BaseAdapter {
private List<VideoViewInfo> items;
LayoutInflater inflater;
/**
* 构造器
*/
public VideoGalleryAdapter(Context context, ArrayList<VideoViewInfo> viewInfos) {
this.items = viewInfos;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (null == convertView) {
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.img_thumb);
holder.textView = (TextView) convertView.findViewById(R.id.txt_info);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String tmpStr = items.get(position).thumbPath;
if (null != tmpStr) {
holder.imageView.setImageURI(Uri.parse(tmpStr));
}
holder.textView.setText(items.get(position).title);
return convertView;
}
class ViewHolder {
ImageView imageView;
TextView textView;
}
}
}
* mediaStore检索视频
*
* @time 下午05:15:41
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class VideoGalleyActivity extends Activity implements OnClickListener, OnItemClickListener {
private ListView listView;
private Cursor cursor;
/**
* 视频文件信息
*/
class VideoViewInfo {
String filePath;
String mimeType;
String thumbPath;
String title;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_gallery);
findView();
init();
getVideo();
}
/**
* 初始化方法
*/
private void init() {
}
/**
* 实例化控件
*/
private void findView() {
listView = (ListView) this.findViewById(R.id.listView);
listView.setOnItemClickListener(this);
}
@Override
public void onClick(View v) {
}
/**
* mediaStore检索视频
*/
private void getVideo() {
String[] mediaColumns = { MediaStore.Video.Media._ID,//
MediaStore.Video.Media.DATA,//
MediaStore.Video.Media.TITLE,//
MediaStore.Video.Media.MIME_TYPE };
String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA,//
MediaStore.Video.Thumbnails.VIDEO_ID //
};
cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);
ArrayList<VideoViewInfo> viewInfos = new ArrayList<VideoGalleyActivity.VideoViewInfo>();
if (cursor.moveToFirst()) {
do {
VideoViewInfo info = new VideoViewInfo();
// 获取缩略图
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media._ID));
String selection = MediaStore.Video.Thumbnails.VIDEO_ID + "=?";
String[] selectionArgs = new String[] { id + "" };
Cursor thumbCursor = this.managedQuery(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, thumbColumns, selection, selectionArgs, null);
if (thumbCursor.moveToFirst()) {
info.thumbPath = thumbCursor.getString(thumbCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
}
info.filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
info.title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.TITLE));
info.mimeType = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.MIME_TYPE));
viewInfos.add(info);
} while (cursor.moveToNext());
}
listView.setAdapter(new VideoGalleryAdapter(VideoGalleyActivity.this, viewInfos));
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (cursor.moveToPosition(position)) {
int fileColum = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
int mimeColum = cursor.getColumnIndex(MediaStore.Video.Media.MIME_TYPE);
String videoFilePath = cursor.getString(fileColum);
String mimeType = cursor.getString(mimeColum);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
File file = new File(videoFilePath);
intent.setDataAndType(Uri.fromFile(file), mimeType);
startActivity(intent);
}
}
/*****************************************/
/**
*
*/
class VideoGalleryAdapter extends BaseAdapter {
private List<VideoViewInfo> items;
LayoutInflater inflater;
/**
* 构造器
*/
public VideoGalleryAdapter(Context context, ArrayList<VideoViewInfo> viewInfos) {
this.items = viewInfos;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (null == convertView) {
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.img_thumb);
holder.textView = (TextView) convertView.findViewById(R.id.txt_info);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String tmpStr = items.get(position).thumbPath;
if (null != tmpStr) {
holder.imageView.setImageURI(Uri.parse(tmpStr));
}
holder.textView.setText(items.get(position).title);
return convertView;
}
class ViewHolder {
ImageView imageView;
TextView textView;
}
}
}