android 视频缩略图之 MediaStore

首先 声明MediaStore是2.0以后才有的类 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>
<?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、我先按照参考的第一个url来写的

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;   

        }   
           
    }   
}
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;
		}
		
	}
}

但是结果是能得到title得不到缩略图貌似大家都说好使但是我这里就是不行很是纳闷

后来我就直接看了下com.android.providers.media下面的external-ef8261f.db

数据库的结构如下图:

这个就不介绍了看下名字应该就明白了

我们直接看video和videothumbnails问题就在这里

video

这里面罗列的sdcard的所有视频文件我们可以得到名字类型持续时间大小等有用的数据

再看一下videothumbnails

这里面是没有数据的!!!这个就是原因了

所以我们根据VIDEO_ID在videothumbnails表里面肯定是什么也得不到的

2、

仔细查看MediaStore.Video.Thumbnails类里面的方法 可以发现 提供了一个getThumbnail()方法 可以直接得到缩略图
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));   


                BitmapFactory.Options options = new BitmapFactory.Options();   


                options.inDither = false;   

                options.inPreferredConfig = Bitmap.Config.ARGB_8888;   
                info.b = MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(), id,  Images.Thumbnails.MICRO_KIND, options);   

                //然后将其加入到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;   
        Bitmap b;   
        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).b != null){   

                holder.thumbImage.setImageBitmap(videoList.get(position).b);   
            }   
               

            return convertView;   

        }   
           
    }   
}
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));
				BitmapFactory.Options options = new BitmapFactory.Options();
	            options.inDither = false;
	            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
				info.b = MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(), id,  Images.Thumbnails.MICRO_KIND, options);
				//然后将其加入到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;
		Bitmap b;
		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).b != null){
				holder.thumbImage.setImageBitmap(videoList.get(position).b);
			}
			
			return convertView;
		}
		
	}
}

哈哈图片显示出来了证明这样是可行的

所以结论是:

1、我们可以通过
managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);
managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);

从video表里面得到关于视频的一些数据,如:时间、大小、类型等

2、我们可以通过
MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(), id,  Images.Thumbnails.MICRO_KIND, options);
MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(), id,  Images.Thumbnails.MICRO_KIND, options);

直接得到缩略图

相关推荐