获取图片缩略图和视频缩略图
获取图片缩略图和视频缩略图的方法:
Java代码:
- import java.io.File;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.media.ThumbnailUtils;
- import android.os.Bundle;
- import android.os.Environment;
- import android.provider.MediaStore;
- import android.widget.ImageView;
- /**
- * 获取图片和视频的缩略图
- * 这两个方法必须在2.2及以上版本使用,因为其中使用了ThumbnailUtils这个类
- */
- public class AndroidTestActivity extends Activity {
- private ImageView imageThumbnail;
- private ImageView videoThumbnail;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- imageThumbnail = (ImageView) findViewById(R.id.image_thumbnail);
- videoThumbnail = (ImageView) findViewById(R.id.video_thumbnail);
- String imagePath = Environment.getExternalStorageDirectory()
- .getAbsolutePath()
- + File.separator
- + "photo"
- + File.separator
- + "yexuan.jpg";
- String videoPath = Environment.getExternalStorageDirectory()
- .getAbsolutePath()
- + File.separator
- + "video"
- + File.separator
- + "醋点灯.avi";
- imageThumbnail.setImageBitmap(getImageThumbnail(imagePath, 60, 60));
- videoThumbnail.setImageBitmap(getVideoThumbnail(videoPath, 60, 60,
- MediaStore.Images.Thumbnails.MICRO_KIND));
- }
- /**
- * 根据指定的图像路径和大小来获取缩略图
- * 此方法有两点好处:
- * 1. 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度,
- * 第二次读取的bitmap是根据比例压缩过的图像,第三次读取的bitmap是所要的缩略图。
- * 2. 缩略图对于原图像来讲没有拉伸,这里使用了2.2版本的新工具ThumbnailUtils,使
- * 用这个工具生成的图像不会被拉伸。
- * @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();
- 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;
- }
- /**
- * 获取视频的缩略图
- * 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。
- * 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。
- * @param videoPath 视频的路径
- * @param width 指定输出视频缩略图的宽度
- * @param height 指定输出视频缩略图的高度度
- * @param kind 参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。
- * 其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96
- * @return 指定大小的视频缩略图
- */
- private Bitmap getVideoThumbnail(String videoPath, int width, int height,
- int kind) {
- Bitmap bitmap = null;
- // 获取视频的缩略图
- bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
- System.out.println("w"+bitmap.getWidth());
- System.out.println("h"+bitmap.getHeight());
- bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
- ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
- return bitmap;
- }
- }
import java.io.File;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.ImageView;
/*** 获取图片和视频的缩略图* 这两个方法必须在2.2及以上版本使用,因为其中使用了ThumbnailUtils这个类*/
public class AndroidTestActivity extends Activity {private ImageView imageThumbnail;private ImageView videoThumbnail;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);imageThumbnail = (ImageView) findViewById(R.id.image_thumbnail);videoThumbnail = (ImageView) findViewById(R.id.video_thumbnail);String imagePath = Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator+ "photo"+ File.separator+ "yexuan.jpg";String videoPath = Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator+ "video"+ File.separator+ "醋点灯.avi";imageThumbnail.setImageBitmap(getImageThumbnail(imagePath, 60, 60));videoThumbnail.setImageBitmap(getVideoThumbnail(videoPath, 60, 60,MediaStore.Images.Thumbnails.MICRO_KIND));}/*** 根据指定的图像路径和大小来获取缩略图* 此方法有两点好处:* 1. 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度,* 第二次读取的bitmap是根据比例压缩过的图像,第三次读取的bitmap是所要的缩略图。* 2. 缩略图对于原图像来讲没有拉伸,这里使用了2.2版本的新工具ThumbnailUtils,使* 用这个工具生成的图像不会被拉伸。* @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();options.inJustDecodeBounds = true;// 获取这个图片的宽和高,注意此处的bitmap为nullbitmap = 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 设为 falsebitmap = BitmapFactory.decodeFile(imagePath, options);// 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,ThumbnailUtils.OPTIONS_RECYCLE_INPUT);return bitmap;}/*** 获取视频的缩略图* 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。* 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。* @param videoPath 视频的路径* @param width 指定输出视频缩略图的宽度* @param height 指定输出视频缩略图的高度度* @param kind 参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。* 其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96* @return 指定大小的视频缩略图*/private Bitmap getVideoThumbnail(String videoPath, int width, int height,int kind) {Bitmap bitmap = null;// 获取视频的缩略图bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);System.out.println("w"+bitmap.getWidth());System.out.println("h"+bitmap.getHeight());bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,ThumbnailUtils.OPTIONS_RECYCLE_INPUT);return bitmap;}}
main.xml文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="图片缩略图" />
- <ImageView
- android:id="@+id/image_thumbnail"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="视频缩略图" />
- <ImageView
- android:id="@+id/video_thumbnail"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="图片缩略图" /><ImageViewandroid:id="@+id/image_thumbnail"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="视频缩略图" /><ImageViewandroid:id="@+id/video_thumbnail"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>
运行后的截图为:
![](https://img-my.csdn.net/uploads/201204/01/1333264599_4654.jpg)
获取图片缩略图和视频缩略图相关推荐
- java 优酷视频缩略图_java获取优酷等视频缩略图
类型:Android平台大小:6.8M语言:中文 评分:7.2 标签: 立即下载 想获取优酷等视频缩略图,在网上没有找到满意的资料,参考了huangdijia的PHP版工具一些思路,写了下面的JAVA ...
- instagram获取图片地址和视频地址
instagram 获取图片地址和视频地址 先保持 以后在详细说明 package mainimport ("bufio""encoding/json"&quo ...
- 使用PHP获取优酷网视频缩略图
function get_youku ( $ url ) { // 这段正则是来获取优酷的id,出处在 /wp-content/languages/zh_CN.php,同样56网.土豆都可以找到 ...
- android异步加载视频缩略图,Android 视频缩略图的缓存机制和异步加载
关注微信号:javalearns 随时随地学Java 或扫一扫 随时随地学Java 在这次的工作开发项目中,涉及到一个视频缩略图的视频列表:这个在大家看来,制作视频缩略图就是两行代码就搞定的事.确 ...
- android获取图片缩略图,Android系获取图片和视频的缩略图
获取手机里视频缩略图: public static Bitmap getVideoThumbnail(ContentResolver cr,Uri uri) { Bitmap bitmap = nul ...
- Android之获取手机上的图片和视频缩略图thumbnails
2019独角兽企业重金招聘Python工程师标准>>> [0]大家都知道Android从1.5开始刚插入SD卡时系统会调用MediaScanner服务进行后台扫描,索引新的歌曲.图片 ...
- android 查询所有图片和视频,Android系统详解之获取图片和视频的缩略图
从Android 2.2开始系统新增了一个缩略图ThumbnailUtils类,位于framework的android.media.ThumbnailUtils位置,可以帮助我们从mediaprovi ...
- android thumbnail获取图片,系统获取视频缩略图的getThumbnail()真的那么快吗?
一:系统提供的一个从数据库中获得缩略图的方法 我在这篇使用七牛云存储上传android本地视频并播放博客中使用了下面的android系统提供的api来获取视频的缩略图 bitmap = MediaSt ...
- 获取图片和视频缩略图Thumbnails ThumbnailUtils
详细请看http://blog.csdn.net/zhou699/article/details/6538478 http://shazhuzhu1.iteye.com/blog/1254724 详细 ...
最新文章
- 如何使用Elasticsearch groovy script脚本更新数据
- vxworks 实时操作系统
- Redis中的List 列表
- thrift简单使用
- [SpringSecurity]基本原理_过滤器链
- c语言命名规则_C语言的基本数据类型及变量
- 李宏毅机器学习(七)Bert and its family
- Python | threading05 - 使用有界信号量,实现线程间同步
- 【前端 · 面试 】HTTP 总结(八)—— HTTP 强缓存
- 九龙山风电场电气一次部分初步设计
- O'Stolz定理的应用
- Unity资源打包(AssetBundle)
- EPLAN中的edz文件的用法
- 中国大学MOOC体育保健学考试试题及答案
- 聚类算法Clustering-KMeans/DBSCAN/DenPeak/NormalizeCut/RCC
- 几何画板如何添加按钮
- 怎么用计算机打出错误,电脑连接打印机怎么一直显示错误怎么办
- 青甘大环线,蓝紫魔仙同游西北
- java android 打地鼠_android实现打地鼠游戏
- 2021年2月16日 星期二 农历初五 晴 天津
热门文章
- 历途机器人给大家拜年喽
- □ 影片名:《唐山大兄》(34)
- 吉首大学计算机科学与技术怎么样,吉首大学是几本 学生评价怎么样好不好(10条)...
- js打印分页、加标题、每页控制条数
- 93.Android申请权限
- linux 分区不够,linux磁盘分区空间不够解决办法
- 使用Java的继承关系来描述动物世界的特征和关系。
- GY906 温度的传感器
- c语言 d1 1,C语言c-1>=d)==1 n – 手机爱问
- u大师u盘装系统win7_u盘怎么安装win7系统 u盘安装win7系统教程【详细介绍】