Android Video Gallery

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

list_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="80dp"
        android:layout_height="80dp" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity implements OnItemClickListener {

	Cursor cursor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ListView listView = (ListView) findViewById(R.id.lv);
		
		String[] thumbCols = { MediaStore.Video.Thumbnails.DATA,
				MediaStore.Video.Thumbnails.VIDEO_ID };

		String[] videoCols = { MediaStore.Video.Media._ID,
				MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE,
				MediaStore.Video.Media.MIME_TYPE };

		cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
				videoCols, null, null, null);

		ArrayList<VideoInfo> videos = new ArrayList<VideoInfo>();

		if (cursor != null && cursor.moveToFirst()) {

			do {
				VideoInfo vf = new VideoInfo();
				int id = cursor.getInt(cursor
						.getColumnIndex(MediaStore.Video.Media._ID));
				
				//查询缩略图
				Cursor thumbCursor = managedQuery(
						MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
						thumbCols, MediaStore.Video.Thumbnails.VIDEO_ID + "="
								+ id, null, null);
				
				if (thumbCursor != null && thumbCursor.moveToFirst()) {

					vf.thumbPath = thumbCursor.getString(thumbCursor
							.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
					Log.v("Debug thumPath:", vf.thumbPath);
				}

				vf.videoPath = cursor.getString(cursor
						.getColumnIndex(MediaStore.Video.Media.DATA));
				vf.title = cursor.getString(cursor
						.getColumnIndex(MediaStore.Video.Media.TITLE));
				vf.mimeType = cursor.getString(cursor
						.getColumnIndex(MediaStore.Video.Media.MIME_TYPE));

				videos.add(vf);

			} while (cursor.moveToNext());

			listView.setAdapter(new VideoGalleryAdapter(this, videos));
			listView.setOnItemClickListener(this);

		}
	}

	private class VideoInfo {
		String videoPath;
		String thumbPath;
		String mimeType;
		String title;
	}

	private class VideoGalleryAdapter extends BaseAdapter {

		private Context context;
		private ArrayList<VideoInfo> videos;

		public VideoGalleryAdapter(Context _context,
				ArrayList<VideoInfo> _videos) {
			context = _context;
			videos = _videos;
		}

		@Override
		public int getCount() {
			return videos.size();
		}

		@Override
		public Object getItem(int position) {
			return videos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
                      //复用同一个View
                      if(convert == null) {

			LayoutInflater inflater;
			// 绑定一个layout xml文件
			inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View row = inflater.inflate(R.layout.list_view, null);
			ImageView thumb = (ImageView) row.findViewById(R.id.imgView);

			if (videos.get(position).thumbPath != null) {
				thumb.setImageURI(Uri.parse(videos.get(position).thumbPath));
			}
			TextView title = (TextView) row.findViewById(R.id.tv);
			title.setText(videos.get(position).title);
                        return row;

                      } else {

                        ImageView thumb = (ImageView) convertView.findViewById(R.id.imgView);

			if (videos.get(position).thumbPath != null) {
				thumb.setImageURI(Uri.parse(videos.get(position).thumbPath));
			}
			TextView title = (TextView) convertView.findViewById(R.id.tv);
			title.setText(videos.get(position).title);
                        return convertView;
                   }	
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

		if (cursor.moveToPosition(arg2)) {
			int filePathIdx = cursor
					.getColumnIndex(MediaStore.Video.Media.DATA);
			int mimeTypeIdx = cursor
					.getColumnIndex(MediaStore.Video.Media.MIME_TYPE);
			String filePath = cursor.getString(filePathIdx);
			String mimeType = cursor.getString(mimeTypeIdx);

			Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
			File file = new File(filePath);
			intent.setDataAndType(Uri.fromFile(file), mimeType);

			startActivity(intent);
		}
	}

}

ref: Pro Android Media: Developing Graphics, Music, Video and Rich Media Apps for Smartphones and Tablets

git clone https://github.com/lnmcc/VideoGallery.git