Android-->原生保存Bitmap(图片),压缩图片,创建图片缩略图和视频缩略图(任意大小)

原图:1728*360 大小:1.1MB

尺寸:20*20 大小:2KB
这里写图片描述

尺寸:60*60 大小:9KB
这里写图片描述

尺寸:100*100 大小:23KB

1:保存Bitmap的方法

/** * 保存Bitmap到文件 */
public static void saveBitmap(Bitmap bmp, String filePath) throws FileNotFoundException {
   FileOutputStream outputStream = new FileOutputStream(new File(filePath));
   bmp.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
}

2:创建缩略图

/** * 创建缩略图 */
public static Bitmap createThumbnail(Bitmap source, int width, int height) {
   return ThumbnailUtils.extractThumbnail(source, width, height);
}

3:创建视频缩略图

/** * 创建视频缩略图 */
public static Bitmap createVideoThumbnail(String filePath, int kind) {
   return ThumbnailUtils.createVideoThumbnail(filePath, kind);
}

//第二个参数只能是以下2个值之一:
//MediaStore.Video.Thumbnails.MINI_KIND;指定缩略图大小为 512 x 384 
//MediaStore.Video.Thumbnails.MICRO_KIND;指定缩略图大小为 96 x 96

方法都是特别简单,难就难在我们并不知道有它的存在.
其实Android系统还为我们提供了很多实用工具类.
如果你发现了非常好用的工具类,欢迎分享给我.

至此: 文章就结束了,如有疑问: QQ群:274306954 欢迎您的加入.

你可能感兴趣的:(压缩,bitmap,图片,video,thumbnail)