获取图片缩略图和视频缩略图的方法:

Java代码:

[java] view plain copy print ?
  1. import java.io.File;
  2. import android.app.Activity;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.media.ThumbnailUtils;
  6. import android.os.Bundle;
  7. import android.os.Environment;
  8. import android.provider.MediaStore;
  9. import android.widget.ImageView;
  10. /**
  11. * 获取图片和视频的缩略图
  12. * 这两个方法必须在2.2及以上版本使用,因为其中使用了ThumbnailUtils这个类
  13. */
  14. public class AndroidTestActivity extends Activity {
  15. private ImageView imageThumbnail;
  16. private ImageView videoThumbnail;
  17. /** Called when the activity is first created. */
  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. imageThumbnail = (ImageView) findViewById(R.id.image_thumbnail);
  23. videoThumbnail = (ImageView) findViewById(R.id.video_thumbnail);
  24. String imagePath = Environment.getExternalStorageDirectory()
  25. .getAbsolutePath()
  26. + File.separator
  27. + "photo"
  28. + File.separator
  29. + "yexuan.jpg";
  30. String videoPath = Environment.getExternalStorageDirectory()
  31. .getAbsolutePath()
  32. + File.separator
  33. + "video"
  34. + File.separator
  35. + "醋点灯.avi";
  36. imageThumbnail.setImageBitmap(getImageThumbnail(imagePath, 60, 60));
  37. videoThumbnail.setImageBitmap(getVideoThumbnail(videoPath, 60, 60,
  38. MediaStore.Images.Thumbnails.MICRO_KIND));
  39. }
  40. /**
  41. * 根据指定的图像路径和大小来获取缩略图
  42. * 此方法有两点好处:
  43. *     1. 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度,
  44. *        第二次读取的bitmap是根据比例压缩过的图像,第三次读取的bitmap是所要的缩略图。
  45. *     2. 缩略图对于原图像来讲没有拉伸,这里使用了2.2版本的新工具ThumbnailUtils,使
  46. *        用这个工具生成的图像不会被拉伸。
  47. * @param imagePath 图像的路径
  48. * @param width 指定输出图像的宽度
  49. * @param height 指定输出图像的高度
  50. * @return 生成的缩略图
  51. */
  52. private Bitmap getImageThumbnail(String imagePath, int width, int height) {
  53. Bitmap bitmap = null;
  54. BitmapFactory.Options options = new BitmapFactory.Options();
  55. options.inJustDecodeBounds = true;
  56. // 获取这个图片的宽和高,注意此处的bitmap为null
  57. bitmap = BitmapFactory.decodeFile(imagePath, options);
  58. options.inJustDecodeBounds = false; // 设为 false
  59. // 计算缩放比
  60. int h = options.outHeight;
  61. int w = options.outWidth;
  62. int beWidth = w / width;
  63. int beHeight = h / height;
  64. int be = 1;
  65. if (beWidth < beHeight) {
  66. be = beWidth;
  67. } else {
  68. be = beHeight;
  69. }
  70. if (be <= 0) {
  71. be = 1;
  72. }
  73. options.inSampleSize = be;
  74. // 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false
  75. bitmap = BitmapFactory.decodeFile(imagePath, options);
  76. // 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象
  77. bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
  78. ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
  79. return bitmap;
  80. }
  81. /**
  82. * 获取视频的缩略图
  83. * 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。
  84. * 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。
  85. * @param videoPath 视频的路径
  86. * @param width 指定输出视频缩略图的宽度
  87. * @param height 指定输出视频缩略图的高度度
  88. * @param kind 参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。
  89. *            其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96
  90. * @return 指定大小的视频缩略图
  91. */
  92. private Bitmap getVideoThumbnail(String videoPath, int width, int height,
  93. int kind) {
  94. Bitmap bitmap = null;
  95. // 获取视频的缩略图
  96. bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
  97. System.out.println("w"+bitmap.getWidth());
  98. System.out.println("h"+bitmap.getHeight());
  99. bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
  100. ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
  101. return bitmap;
  102. }
  103. }
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文件:

[java] view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="图片缩略图" />
  10. <ImageView
  11. android:id="@+id/image_thumbnail"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content" />
  14. <TextView
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:text="视频缩略图" />
  18. <ImageView
  19. android:id="@+id/video_thumbnail"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content" />
  22. </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>

运行后的截图为:

获取图片缩略图和视频缩略图相关推荐

  1. java 优酷视频缩略图_java获取优酷等视频缩略图

    类型:Android平台大小:6.8M语言:中文 评分:7.2 标签: 立即下载 想获取优酷等视频缩略图,在网上没有找到满意的资料,参考了huangdijia的PHP版工具一些思路,写了下面的JAVA ...

  2. instagram获取图片地址和视频地址

    instagram 获取图片地址和视频地址 先保持 以后在详细说明 package mainimport ("bufio""encoding/json"&quo ...

  3. 使用PHP获取优酷网视频缩略图

    function get_youku ( $ url ) { // 这段正则是来获取优酷的id,出处在 /wp-content/languages/zh_CN.php,同样56网.土豆都可以找到   ...

  4. android异步加载视频缩略图,Android 视频缩略图的缓存机制和异步加载

    关注微信号:javalearns   随时随地学Java 或扫一扫 随时随地学Java 在这次的工作开发项目中,涉及到一个视频缩略图的视频列表:这个在大家看来,制作视频缩略图就是两行代码就搞定的事.确 ...

  5. android获取图片缩略图,Android系获取图片和视频的缩略图

    获取手机里视频缩略图: public static Bitmap getVideoThumbnail(ContentResolver cr,Uri uri) { Bitmap bitmap = nul ...

  6. Android之获取手机上的图片和视频缩略图thumbnails

    2019独角兽企业重金招聘Python工程师标准>>> [0]大家都知道Android从1.5开始刚插入SD卡时系统会调用MediaScanner服务进行后台扫描,索引新的歌曲.图片 ...

  7. android 查询所有图片和视频,Android系统详解之获取图片和视频的缩略图

    从Android 2.2开始系统新增了一个缩略图ThumbnailUtils类,位于framework的android.media.ThumbnailUtils位置,可以帮助我们从mediaprovi ...

  8. android thumbnail获取图片,系统获取视频缩略图的getThumbnail()真的那么快吗?

    一:系统提供的一个从数据库中获得缩略图的方法 我在这篇使用七牛云存储上传android本地视频并播放博客中使用了下面的android系统提供的api来获取视频的缩略图 bitmap = MediaSt ...

  9. 获取图片和视频缩略图Thumbnails ThumbnailUtils

    详细请看http://blog.csdn.net/zhou699/article/details/6538478 http://shazhuzhu1.iteye.com/blog/1254724 详细 ...

最新文章

  1. 如何使用Elasticsearch groovy script脚本更新数据
  2. vxworks 实时操作系统
  3. Redis中的List 列表
  4. thrift简单使用
  5. [SpringSecurity]基本原理_过滤器链
  6. c语言命名规则_C语言的基本数据类型及变量
  7. 李宏毅机器学习(七)Bert and its family
  8. Python | threading05 - 使用有界信号量,实现线程间同步
  9. 【前端 · 面试 】HTTP 总结(八)—— HTTP 强缓存
  10. 九龙山风电场电气一次部分初步设计
  11. O'Stolz定理的应用
  12. Unity资源打包(AssetBundle)
  13. EPLAN中的edz文件的用法
  14. 中国大学MOOC体育保健学考试试题及答案
  15. 聚类算法Clustering-KMeans/DBSCAN/DenPeak/NormalizeCut/RCC
  16. 几何画板如何添加按钮
  17. 怎么用计算机打出错误,电脑连接打印机怎么一直显示错误怎么办
  18. 青甘大环线,蓝紫魔仙同游西北
  19. java android 打地鼠_android实现打地鼠游戏
  20. 2021年2月16日 星期二 农历初五 晴 天津

热门文章

  1. 历途机器人给大家拜年喽
  2. □ 影片名:《唐山大兄》(34)
  3. 吉首大学计算机科学与技术怎么样,吉首大学是几本 学生评价怎么样好不好(10条)...
  4. js打印分页、加标题、每页控制条数
  5. 93.Android申请权限
  6. linux 分区不够,linux磁盘分区空间不够解决办法
  7. 使用Java的继承关系来描述动物世界的特征和关系。
  8. GY906 温度的传感器
  9. c语言 d1 1,C语言c-1>=d)==1 n – 手机爱问
  10. u大师u盘装系统win7_u盘怎么安装win7系统 u盘安装win7系统教程【详细介绍】