本文共 2050 字,大约阅读时间需要 6 分钟。
利用ThumbnailUtils来实现获取图片和视频的缩略图
利用ThumbnailUtils的extractThumbnail()方法来实现
1. static Bitmap extractThumbnail(Bitmap source, int width, int height, int options) //直接对Bitmap进行缩略操作,最后一个参数定义为OPTIONS_RECYCLE_INPUT ,来回收资源 2. static Bitmap extractThumbnail(Bitmap source, int width, int height) // 这个和上面的方法一样,无options选项private Bitmap getImageThumbnail(String imagePath, int width, int height) { Bitmap bitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 获取这个图片的宽和高,注意此处的bitmap为null bitmap = BitmapFactory.decodeFile(imagePath, options); options.inJustDecodeBounds = false; // 设为 false // 计算缩放比 int h = options.outHeight; int w = options.outWidth; int beWidth = w / width; int beHeight = h / height; int be = 1; if (beWidth < beHeight) { be = beWidth; } else { be = beHeight; } if (be <= 0) { be = 1; } options.inSampleSize = be; // 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false bitmap = BitmapFactory.decodeFile(imagePath, options); // 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象 bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); return bitmap; }
public static Bitmap getVideoThumbnail(String filePath, int width_, int height_, int kind) { Bitmap bitmap = null; try { bitmap = ThumbnailUtils.createVideoThumbnail(filePath,kind); bitmap = ThumbnailUtils.extractThumbnail(bitmap, width_, height_, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); } catch (Exception e) { e.printStackTrace(); } if (bitmap == null) return null; return bitmap; }
项目源码地址
CSDN: GitHub:转载地址:http://holji.baihongyu.com/