mainActivity:
package com.sxt.day07_09; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.os.Bundle; import android.provider.MediaStore.Video; import android.provider.MediaStore.Video.Thumbnails; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.TextView; import android.widget.VideoView; public class MainActivity extends Activity { ArrayList<HashMap<String, Object>> mVideos;//M Gallery mGallery;//V VideoAdapter mAdapter;//C,用于列表显示 VideoView mVideoView;//预览视频控件 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getVideoInfo(); initView(); setListener(); } private void setListener() { mGallery.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { HashMap<String, Object> video=mVideos.get(position); String path=video.get(Video.Media.DATA).toString(); mVideoView.setVideoPath(path);//设置视频路径 mVideoView.start();//播放 } }); } private void initView() { mVideoView=(VideoView) findViewById(R.id.vv); mGallery=(Gallery) findViewById(R.id.gallery); mAdapter=new VideoAdapter(mVideos, this);//M添加到 mGallery.setAdapter(mAdapter); } private void getVideoInfo() { ContentResolver resolver = getContentResolver(); String[] projection={ Video.Media._ID,//拿到略缩图 Video.Media.DATA,//sd卡路径 Video.Media.TITLE }; mVideos=new ArrayList<HashMap<String,Object>>(); //查询的是安卓数据库,数据库存的是多媒体文件的信息 Cursor c = resolver.query(Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, null); while(c.moveToNext()){ HashMap<String, Object> video=new HashMap<String, Object>(); video.put(Video.Media._ID, c.getInt(c.getColumnIndex(Video.Media._ID))); video.put(Video.Media.DATA, c.getString(c.getColumnIndex(Video.Media.DATA))); video.put(Video.Media.TITLE, c.getString(c.getColumnIndex(Video.Media.TITLE))); mVideos.add(video); } } //适配器,用于列表显示 class VideoAdapter extends BaseAdapter{ ArrayList<HashMap<String, Object>> videos; Context context; public VideoAdapter(ArrayList<HashMap<String, Object>> videos, Context context) { super(); this.videos = videos; this.context = context; } @Override public int getCount() { return videos.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override //没有用缓存做优化 public View getView(int position, View layout, ViewGroup parent) { layout=View.inflate(context, R.layout.item_video, null); ImageView ivThumb=(ImageView) layout.findViewById(R.id.ivThumb);//显示视频略缩图 TextView tvTitle=(TextView) layout.findViewById(R.id.tvTitle);//视频标题 HashMap<String, Object> video=videos.get(position); tvTitle.setText(video.get(Video.Media.TITLE).toString()); int id=(Integer) video.get(Video.Media._ID); //Thumbnails类用于获取视频或者图片的略缩图, Bitmap bm=Thumbnails.getThumbnail(getContentResolver(), id, Thumbnails.MICRO_KIND, null); ivThumb.setImageBitmap(bm); return layout; } } }
主页面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Gallery 水平滚动 android:id="@+id/gallery" android:layout_width="match_parent" android:layout_height="wrap_content" /> <VideoView 预览播放视频控件 android:id="@+id/vv" android:layout_below="@id/gallery" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> item_view.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView 每一项布局 android:id="@+id/ivThumb" android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/tvTitle" android:layout_width="80dp" android:layout_height="wrap_content" android:text="安卓" android:gravity="center_horizontal"/> </LinearLayout>
本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/4909315.html,如需转载请自行联系原作者