博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android获取本地图片、视频缩略图
阅读量:4069 次
发布时间:2019-05-25

本文共 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;    }

获取视频的缩略图

  1. 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:

转载地址:http://holji.baihongyu.com/

你可能感兴趣的文章
IO口的作用
查看>>
UIView的使用setNeedsDisplay
查看>>
归档与解归档
查看>>
Window
查看>>
为什么button在设置标题时要用一个方法,而不像lable一样直接用一个属性
查看>>
字符串的截取
查看>>
2. Add Two Numbers
查看>>
17. Letter Combinations of a Phone Number (DFS, String)
查看>>
93. Restore IP Addresses (DFS, String)
查看>>
19. Remove Nth Node From End of List (双指针)
查看>>
49. Group Anagrams (String, Map)
查看>>
139. Word Break (DP)
查看>>
23. Merge k Sorted Lists (Divide and conquer, Linked List) 以及java匿名内部类
查看>>
Tensorflow入门资料
查看>>
剑指_用两个栈实现队列
查看>>
剑指_顺时针打印矩阵
查看>>
剑指_栈的压入弹出序列
查看>>
剑指_复杂链表的复制
查看>>
服务器普通用户(非管理员账户)在自己目录下安装TensorFlow
查看>>
星环后台研发实习面经
查看>>