Android编程获取图片和视频缩略图的方法

2019-12-10 18:19:43丽君
易采站长站为您分析Android编程获取图片和视频缩略图的方法,结合实例形式分析了Android图形图像处理所涉及的常用函数与使用技巧,需要的朋友可以参考下  

本文实例讲述了Android编程获取图片和视频缩略图的方法。,具体如下:

从Android 2.2开始系统新增了一个缩略图ThumbnailUtils类,位于framework的android.media.ThumbnailUtils位 置,可以帮助我们从mediaprovider中获取系统中的视频或图片文件的缩略图,该类提供了三种静态方法可以直接调用获取。

1. createVideoThumbnail

static Bitmap createVideoThumbnail(String filePath, int kind)
//获取视频文件的缩略图
//第一个参数为视频文件的位置,比如/sdcard/android123.3gp,
//第二个参数可以为MINI_KIND或 MICRO_KIND最终和分辨率有关

2. extractThumbnail

static Bitmap extractThumbnail(Bitmap source, int width, int height, int options)
//直接对Bitmap进行缩略操作
//最后一个参数定义为OPTIONS_RECYCLE_INPUT ,来回收资源

3. extractThumbnail

static Bitmap extractThumbnail(Bitmap source, int width, int height)
// 这个和上面的方法一样,无options选项

获取手机里视频缩略图:

public static Bitmap getVideoThumbnail(ContentResolver cr, Uri uri) {
  Bitmap bitmap = null;
  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inDither = false;
  options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  Cursor cursor = cr.query(uri,new String[] { MediaStore.Video.Media._ID }, null, null, null);
  if (cursor == null || cursor.getCount() == 0) {
 return null;
  }
  cursor.moveToFirst();
  String videoId = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media._ID)); //image id in image table.s
  if (videoId == null) {
  return null;
  }
  cursor.close();
  long videoIdLong = Long.parseLong(videoId);
  bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr, videoIdLong,Images.Thumbnails.MICRO_KIND, options);
  return bitmap;
}