歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android 視頻縮略圖之 MediaStore

首先 聲明MediaStore是2.0以後才有的類

Android 視頻縮略圖之 MediaStore代碼下載:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /pub/Android源碼集錦/2011年/12月/Android 視頻縮略圖之 MediaStore/

listview.xml如下

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <ImageView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:id="@+id/iv"  
  11.         />  
  12.     <TextView    
  13.         android:layout_width="fill_parent"   
  14.         android:layout_height="wrap_content"  
  15.         android:id="@+id/tv"   
  16.         />  
  17. </LinearLayout>  

1、

 

  1. package com.lp;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.ListActivity;  
  6. import android.content.Context;  
  7. import android.database.Cursor;  
  8. import android.net.Uri;  
  9. import android.os.Bundle;  
  10. import android.provider.MediaStore;  
  11. import android.view.LayoutInflater;  
  12. import android.view.View;  
  13. import android.view.ViewGroup;  
  14. import android.widget.BaseAdapter;  
  15. import android.widget.ImageView;  
  16. import android.widget.TextView;  
  17. /** 
  18.  * 本實例,我們將通過檢索SDCard上的Video信息 
  19.  * 在MediaStore中,MediaStore.Video.Media中就有Video相關信息, 
  20.  * 同時MediaStore.Video.Thumbnails中含有各個video對應的縮略圖信息 
  21.  *  
  22.  * @author Administrator 
  23.  * 
  24.  */  
  25. public class MainActivity extends ListActivity {  
  26.       
  27.     private Cursor cursor;  
  28.     private ArrayList<VideoInfo> videoList = new ArrayList<MainActivity.VideoInfo>();  
  29.     private ImageView img;  
  30.       
  31.     public void onCreate(Bundle savedInstanceState){  
  32.         super.onCreate(savedInstanceState);  
  33.         init();  
  34.     }  
  35.       
  36.       
  37.     private void init(){  
  38.         String[] thumbColumns = new String[]{  
  39.                 MediaStore.Video.Thumbnails.DATA,  
  40.                 MediaStore.Video.Thumbnails.VIDEO_ID  
  41.         };  
  42.           
  43.         String[] mediaColumns = new String[]{  
  44.                 MediaStore.Video.Media.DATA,  
  45.                 MediaStore.Video.Media._ID,  
  46.                 MediaStore.Video.Media.TITLE,  
  47.                 MediaStore.Video.Media.MIME_TYPE  
  48.         };  
  49.           
  50.         //首先檢索SDcard上所有的video   
  51.         cursor = this.managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, nullnullnull);  
  52.           
  53.           
  54.         while(cursor.moveToNext()){  
  55.                 VideoInfo info = new VideoInfo();  
  56.                   
  57.                 info.filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));  
  58.                 info.mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));  
  59.                 info.title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));  
  60.                   
  61.                 //獲取當前Video對應的Id,然後根據該ID獲取其Thumb   
  62.                 int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));  
  63.                 String selection = MediaStore.Video.Thumbnails.VIDEO_ID +"=?";  
  64.                 String[] selectionArgs = new String[]{  
  65.                         id+""  
  66.                 };  
  67.                 Cursor thumbCursor = this.managedQuery(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, thumbColumns, selection, selectionArgs, null);  
  68.                   
  69.                 if(thumbCursor.moveToFirst()){  
  70.                     info.thumbPath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA));  
  71.                       
  72.                 }  
  73.                 //然後將其加入到videoList   
  74.                 videoList.add(info);  
  75.                   
  76.         }  
  77.         System.out.println(videoList.size());  
  78.         //然後需要設置ListView的Adapter了,使用我們自定義的Adatper   
  79.         VideoAdapter adapter = new VideoAdapter(this);  
  80.         this.getListView().setAdapter(adapter);  
  81.     }  
  82.       
  83.       
  84.     class VideoInfo{  
  85.         String filePath;  
  86.         String mimeType;  
  87.         String thumbPath;  
  88.         String title;  
  89.     }  
  90.       
  91.     class ViewHolder{  
  92.         ImageView thumbImage;  
  93.         TextView titleText;  
  94.     }  
  95.       
  96.     /** 
  97.      * 定義一個Adapter來顯示縮略圖和視頻title信息 
  98.      * @author Administrator 
  99.      * 
  100.      */  
  101.     private class VideoAdapter extends BaseAdapter{  
  102.           
  103.         private Context mContext;  
  104.         private LayoutInflater inflater;  
  105.           
  106.         public VideoAdapter(Context context){  
  107.             this.mContext = context;  
  108.             this.inflater = LayoutInflater.from(context);   
  109.             this.inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
  110.         }  
  111.         @Override  
  112.         public int getCount() {  
  113.             // TODO Auto-generated method stub   
  114.             return videoList.size();  
  115.         }  
  116.         @Override  
  117.         public Object getItem(int p) {  
  118.             // TODO Auto-generated method stub   
  119.             return videoList.get(p);  
  120.         }  
  121.         @Override  
  122.         public long getItemId(int p) {  
  123.             // TODO Auto-generated method stub   
  124.             return p;  
  125.         }  
  126.         @Override  
  127.         public View getView(int position, View convertView, ViewGroup parent) {  
  128.             ViewHolder holder ;  
  129.             if(convertView == null){  
  130.                 holder = new ViewHolder();  
  131.                 convertView = inflater.inflate(R.layout.listview, null);  
  132.                 holder.thumbImage = (ImageView)convertView.findViewById(R.id.iv);  
  133.                 holder.titleText = (TextView)convertView.findViewById(R.id.tv);  
  134.                 convertView.setTag(holder);  
  135.             }else{  
  136.                 holder = (ViewHolder)convertView.getTag();  
  137.             }  
  138.               
  139.             //顯示信息   
  140.             holder.titleText.setText(videoList.get(position).title);  
  141.             if(videoList.get(position).thumbPath != null){  
  142.                 holder.thumbImage.setImageURI(Uri.parse(videoList.get(position).thumbPath));  
  143.             }  
  144.               
  145.             return convertView;  
  146.         }  
  147.           
  148.     }  
  149. }  
Copyright © Linux教程網 All Rights Reserved