Android获取本地图片、视频缩略图
2016-04-08 17:06
603 查看
利用ThumbnailUtils来实现获取图片和视频的缩略图
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选项
//获取视频文件的缩略图,第一个参数为视频文件的位置,比如/sdcard/android123.3gp,而第二个参数可以为MINI_KIND或 MICRO_KIND最终和分辨率有关
项目源码地址
CSDN:
GitHub:
获取图片缩略图
利用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; }
获取视频的缩略图
static Bitmap createVideoThumbnail(String filePath, int kind)//获取视频文件的缩略图,第一个参数为视频文件的位置,比如/sdcard/android123.3gp,而第二个参数可以为MINI_KIND或 MICRO_KIND最终和分辨率有关
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:
相关文章推荐
- Android 监听Wifi状态和信号强度
- 安卓里获取默认应用方法
- 安卓里获取默认应用方法
- Android NativeActivity原理
- Android Manifest.xml 结构详解
- 黑马最新android学习视频
- Android Studio下生成jar以及aar
- Android 聊天界面背景图片不顶上去内容顶上去解决方法
- Android——Handler 多线程
- Android学习全套视频(64G)
- Android中Fragment学习笔记
- Android存储之ShredPreferences
- Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用
- 2016-4-8工作日志:Android获取WiFi信号强度
- 异常摘要
- Android ORM框架 greenDao使用
- android项目总结
- android 应用退到后台,类似最小化moveTaskToBack(),判断是否是task的根isTaskRoot()
- Android开发小技巧之对android颜色渐变(Gradient )的改进
- Android Studio 推荐安装插件