首先 聲明MediaStore是2.0以後才有的類
Android 視頻縮略圖之 MediaStore代碼下載:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /pub/Android源碼集錦/2011年/12月/Android 視頻縮略圖之 MediaStore/
listview.xml如下
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/iv"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/tv"
- />
- </LinearLayout>
1、
- package com.lp;
-
- import java.util.ArrayList;
-
- import android.app.ListActivity;
- import android.content.Context;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.MediaStore;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- /**
- * 本實例,我們將通過檢索SDCard上的Video信息
- * 在MediaStore中,MediaStore.Video.Media中就有Video相關信息,
- * 同時MediaStore.Video.Thumbnails中含有各個video對應的縮略圖信息
- *
- * @author Administrator
- *
- */
- public class MainActivity extends ListActivity {
-
- private Cursor cursor;
- private ArrayList<VideoInfo> videoList = new ArrayList<MainActivity.VideoInfo>();
- private ImageView img;
-
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- init();
- }
-
-
- private void init(){
- String[] thumbColumns = new String[]{
- MediaStore.Video.Thumbnails.DATA,
- MediaStore.Video.Thumbnails.VIDEO_ID
- };
-
- String[] mediaColumns = new String[]{
- MediaStore.Video.Media.DATA,
- MediaStore.Video.Media._ID,
- MediaStore.Video.Media.TITLE,
- MediaStore.Video.Media.MIME_TYPE
- };
-
- //首先檢索SDcard上所有的video
- cursor = this.managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);
-
-
- while(cursor.moveToNext()){
- VideoInfo info = new VideoInfo();
-
- info.filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
- info.mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
- info.title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
-
- //獲取當前Video對應的Id,然後根據該ID獲取其Thumb
- int id = cursor.getInt(cursor.getColumnIndexOrThrow(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 = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA));
-
- }
- //然後將其加入到videoList
- videoList.add(info);
-
- }
- System.out.println(videoList.size());
- //然後需要設置ListView的Adapter了,使用我們自定義的Adatper
- VideoAdapter adapter = new VideoAdapter(this);
- this.getListView().setAdapter(adapter);
- }
-
-
- class VideoInfo{
- String filePath;
- String mimeType;
- String thumbPath;
- String title;
- }
-
- class ViewHolder{
- ImageView thumbImage;
- TextView titleText;
- }
-
- /**
- * 定義一個Adapter來顯示縮略圖和視頻title信息
- * @author Administrator
- *
- */
- private class VideoAdapter extends BaseAdapter{
-
- private Context mContext;
- private LayoutInflater inflater;
-
- public VideoAdapter(Context context){
- this.mContext = context;
- this.inflater = LayoutInflater.from(context);
- this.inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return videoList.size();
- }
- @Override
- public Object getItem(int p) {
- // TODO Auto-generated method stub
- return videoList.get(p);
- }
- @Override
- public long getItemId(int p) {
- // TODO Auto-generated method stub
- return p;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder ;
- if(convertView == null){
- holder = new ViewHolder();
- convertView = inflater.inflate(R.layout.listview, null);
- holder.thumbImage = (ImageView)convertView.findViewById(R.id.iv);
- holder.titleText = (TextView)convertView.findViewById(R.id.tv);
- convertView.setTag(holder);
- }else{
- holder = (ViewHolder)convertView.getTag();
- }
-
- //顯示信息
- holder.titleText.setText(videoList.get(position).title);
- if(videoList.get(position).thumbPath != null){
- holder.thumbImage.setImageURI(Uri.parse(videoList.get(position).thumbPath));
- }
-
- return convertView;
- }
-
- }
- }