android中关于视频、音频、图片的缩略图索引
android开发中关于视频、音频、图片的缩略图索引
Android 从1.5开始刚插入SD卡时系统会调用MediaScanner服务进行后台扫描,索引新的歌曲、图片和视频等信息,
1、 快速提取图片和视频缩略图可以直接访问
android.provider.MediaStore.Images.Thumbnails
android.provider.MediaStore.Video.Thumbnails
这两个数据库,即可查询出来缩略图
有关具体的缩略图可以通过一下方式
getThumbnail(ContentResolver cr, long origId, int kind, BitmapFactory.Options options)
getThumbnail(ContentResolver cr, long origId, long groupId, int kind, BitmapFactory.Options options)
方法获取,这两种方法返回Bitmap类型,而缩略图的分辨率可以从HEIGHT和WIDTH两个字段提取,在Android上缩略图分为两种,通过读取KIND字段来获得,分别为
MICRO_KIND和MINI_KIND 分别为微型和迷你两种缩略模式
另一种方法从Android2.2开始系统新增了一个缩略图ThumbnailUtils类
1. static Bitmap createVideoThumbnail(String filePath, int kind)
//获取视频文件的缩略图,第一个参数为视频文件的位置,比如/sdcard/android123.3gp,而第二个参数可以为 MINI_KIND或MICRO_KIND最终和分辨率有关
2. static Bitmap extractThumbnail(Bitmap source, int width, int height, int options)
//直接对Bitmap进行缩略操作,最后一个参数定义为OPTIONS_RECYCLE_INPUT,来回收资源
3. static Bitmap extractThumbnail(Bitmap source, int width, int height)
2、具体操作
android 获取视频和图片的缩略图、图片对应表images和thumbnails
images
_id 图片id
_data 图片路径
thumbnails
_id 缩略图id
_data 缩略图存储位置
image_id 对应原图id
视频对应表images和thumbnails
video
_id 视频id
_data 视频路径
videothumbnails
_id 视频缩略图id
_data 视频缩略图存储位置
image_id 对应视频id
//========================================================================
3 以获取视频的缩略图为例,具体步骤如下:
1 获取视频的uri
2 转换为String,得到视频id
3 去videothumbnails表中找到视频缩略图路径
4 根据路径得到图片(并绑定到listview上)
<配置文件> 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>
显示Activity::MainActivity.java
package com.lp; import java.util.ArrayList; import android.app.ListActivity; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.provider.MediaStore.Images; 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(){ //MediaStore.Video.Thumbnails中含有各个video对应的缩略图信息 String[] thumbColumns = new String[]{ MediaStore.Video.Thumbnails.DATA, MediaStore.Video.Thumbnails.VIDEO_ID }; //MediaStore.Video.Media中就有Video相关信息 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); 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); } private static final String[] GENRES = new String[] { "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama", "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller" }; class VideoInfo{ String filePath; String mimeType; // String thumbPath; 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) 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; } } }
还有的人采用解码视频第一帧作为缩略图,大家可以了解一下,至于音乐和图片则比较简单
继续关注Android开发系类笔记。