有时我们的app需要批量的展示图片,或者视频文件,不管是以listview还是gridview等等,我们都是需要拿到图片数据的。
而且为了避免OOM以及减少没必要的开销,我们更倾向于显示缩略图。
本篇就简单介绍下使用ThumbnailUtils类获取缩略图。
首先呢,我们需要用到contentprovider获取相应文件的路径,这个就不赘述了,直接进入正题
即:在已有文件路径、文件类型的情况下得到想要的缩略图
/**
* @Title: getImageThumbnail
* @Description: 获取指定路径的图片的缩略图
* @author: leobert.lan
* @param imagePath
* 文件路径
* @param width
* 缩略图的宽度
* @param height
* 缩略图的高度
* @return
*/
private Bitmap getImageThumbnail(String imagePath, int width, int height) {
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
/*
* inJustDecodeBounds: If set to true, the decoder will return null (no
* bitmap), but the out... fields will still be set, allowing the caller
* to query the bitmap without having to allocate the memory for its
* pixels.
*/
options.inJustDecodeBounds = true;
// 获取图片的宽、高,但是:bitmap为null!可以看一下上面的内容体会一下
bitmap = BitmapFactory.decodeFile(imagePath, options);
// 计算缩放比
int h = options.outHeight;
int w = options.outWidth;
int beWidth = w / width;
int beHeight = h / height;
int be = 1;
be = Math.min(beHeight, beWidth);
if (be <= 0) {
be = 1;
}
options.inSampleSize = be;
options.inJustDecodeBounds = false;
// 真正读入图片
bitmap = BitmapFactory.decodeFile(imagePath, options);
// 利用ThumbnailUtils 根据原来的图创建缩略图
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
return bitmap;
}
而获取视频的,要仔细点哦,毕竟视频是一连串的图片,不是一张图片
/**
* @Title: getVideoThumbnail
* @Description: 获取指定路径的视频的缩略图
* @author: leobert.lan
* @param videoPath
* 视频路径
* @param width
*
* @param height
*
* @param kind
* kind could be MINI_KIND or MICRO_KIND
* @return
*/
private Bitmap getVideoThumbnail(String videoPath, int width, int height,
int kind) {
Bitmap bitmap = null;
// 获取视频的缩略图
// kind could be MINI_KIND or MICRO_KIND
bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
return bitmap;
}