该文章是为了检索手机上sd卡中的视频,然后将检索出来的相应视频的缩略图,名称等视频信息显示在ListView上。点击每个item后播放相应的视频。
源代码:
布局文件:
activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java:
package com.multimediademo13mediastore; import java.io.File; import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; public class MainActivity extends Activity implements OnItemClickListener { private ListView listView; private Cursor cursor; @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); /** * 从MediaStore.Video.Thumbnail查询中获得的列的列表。 */ String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA, MediaStore.Video.Thumbnails.VIDEO_ID }; /** * 从MediaStore.Video.Media查询中获得的列的列表。 */ String[] mediaColumns = { MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE, MediaStore.Video.Media.MIME_TYPE }; /** * 在主查询中将选择所有在MediaStore中表示的视频 */ cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null); ArrayList<VideoViewInfo> videoRows = new ArrayList<VideoViewInfo>(); if (cursor.moveToFirst()) { do { VideoViewInfo newVVI = new VideoViewInfo(); /** * 将使用另一个查询为每个视频提取缩略图,而且这些数据块都将存储在VideoViewInfo对象中。 */ int id = cursor.getInt(cursor .getColumnIndex(MediaStore.Video.Media._ID)); Cursor thumbCursor = managedQuery( MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID + "=" + id, null, null); if (thumbCursor.moveToFirst()) { newVVI.thumbPath = thumbCursor.getString(thumbCursor .getColumnIndex(MediaStore.Video.Thumbnails.DATA)); } newVVI.filePath = cursor.getString(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.DATA)); newVVI.title = cursor.getString(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE)); newVVI.mimeType = cursor .getString(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE)); videoRows.add(newVVI); } while (cursor.moveToNext()); } listView.setAdapter(new VideoGalleryAdapter(this, videoRows)); listView.setOnItemClickListener(this); } /** * 这个方法将从Cursor对象中提取所需的数据,点击一个item,将创建一个意图,以启动手机设备上默认的媒体播放器来播放该item视频。 */ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (cursor.moveToPosition(position)) { int fileColumn = cursor .getColumnIndexOrThrow(MediaStore.Video.Media.DATA); int mimeColumn = cursor .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE); String videoFilePath = cursor.getString(fileColumn); String mimeType = cursor.getString(mimeColumn); Intent intent = new Intent(android.content.Intent.ACTION_VIEW); File newFile = new File(videoFilePath); intent.setDataAndType(Uri.fromFile(newFile), mimeType); startActivity(intent); } } }
package com.multimediademo13mediastore; public class VideoViewInfo { String filePath; String mimeType; String thumbPath; String title; }VideoGalleryAdapter.java:
package com.multimediademo13mediastore; import java.util.ArrayList; import android.content.Context; import android.net.Uri; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; /** * ListView适配器 * @author Administrator * */ public class VideoGalleryAdapter extends BaseAdapter{ private Context context; private ArrayList<VideoViewInfo> videoRows; LayoutInflater inflater; public VideoGalleryAdapter(Context context, ArrayList<VideoViewInfo> videoRows) { this.context = context; this.videoRows = videoRows; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return videoRows.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View videoRow = inflater.inflate(R.layout.list_item, null); ImageView videoThumb = (ImageView) videoRow.findViewById(R.id.imageView); if (videoRows.get(position).thumbPath != null) { videoThumb.setImageURI(Uri.parse(videoRows.get(position).thumbPath)); } TextView videoTitle = (TextView) videoRow.findViewById(R.id.textView); videoTitle.setText(videoRows.get(position).title); return videoRow; } }
源码下载:
Android MediaStore检索视频并播放,布布扣,bubuko.com
原文:http://blog.csdn.net/u012440207/article/details/27357011