项目中常常遇到文件压缩问题,上传文件大小限制

今天简单的分享一点干货,文件压缩,图片压缩,压缩Bitmap

主要通过尺寸压缩和质量压缩,以达到清晰度最优

效果图

源码地址: https://github.com/DickyQie/android-util

工具类代码

  1. public class CompressHelper {
  2. private static volatile CompressHelper INSTANCE;
  3.  
  4. private Context context;
  5. /**
  6. * 最大宽度,默认为720
  7. */
  8. private float maxWidth = 720.0f;
  9. /**
  10. * 最大高度,默认为960
  11. */
  12. private float maxHeight = 960.0f;
  13. /**
  14. * 默认压缩后的方式为JPEG
  15. */
  16. private Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
  17.  
  18. /**
  19. * 默认的图片处理方式是ARGB_8888
  20. */
  21. private Bitmap.Config bitmapConfig = Bitmap.Config.ARGB_8888;
  22. /**
  23. * 默认压缩质量为80
  24. */
  25. private int quality = 80;
  26. /**
  27. * 存储路径
  28. */
  29. private String destinationDirectoryPath;
  30. /**
  31. * 文件名前缀
  32. */
  33. private String fileNamePrefix;
  34. /**
  35. * 文件名
  36. */
  37. private String fileName;
  38.  
  39. public static CompressHelper getDefault(Context context) {
  40. if (INSTANCE == null) {
  41. synchronized (CompressHelper.class) {
  42. if (INSTANCE == null) {
  43. INSTANCE = new CompressHelper(context);
  44. }
  45. }
  46. }
  47. return INSTANCE;
  48. }
  49.  
  50. private CompressHelper(Context context) {
  51. this.context = context;
  52. destinationDirectoryPath = context.getCacheDir().getPath() + File.pathSeparator + FileUtil.FILES_PATH;
  53. }
  54.  
  55. /**
  56. * 压缩成文件
  57. * @param file 原始文件
  58. * @return 压缩后的文件
  59. */
  60. public File compressToFile(File file) {
  61. return BitmapUtil.compressImage(context, Uri.fromFile(file), maxWidth, maxHeight,
  62. compressFormat, bitmapConfig, quality, destinationDirectoryPath,
  63. fileNamePrefix, fileName);
  64. }
  65.  
  66. /**
  67. * 压缩为Bitmap
  68. * @param file 原始文件
  69. * @return 压缩后的Bitmap
  70. */
  71. public Bitmap compressToBitmap(File file) {
  72. return BitmapUtil.getScaledBitmap(context, Uri.fromFile(file), maxWidth, maxHeight, bitmapConfig);
  73. }
  74.  
  75. /**
  76. * 采用建造者模式,设置Builder
  77. */
  78. public static class Builder {
  79. private CompressHelper mCompressHelper;
  80.  
  81. public Builder(Context context) {
  82. mCompressHelper = new CompressHelper(context);
  83. }
  84.  
  85. /**
  86. * 设置图片最大宽度
  87. * @param maxWidth 最大宽度
  88. */
  89. public Builder setMaxWidth(float maxWidth) {
  90. mCompressHelper.maxWidth = maxWidth;
  91. return this;
  92. }
  93.  
  94. /**
  95. * 设置图片最大高度
  96. * @param maxHeight 最大高度
  97. */
  98. public Builder setMaxHeight(float maxHeight) {
  99. mCompressHelper.maxHeight = maxHeight;
  100. return this;
  101. }
  102.  
  103. /**
  104. * 设置压缩的后缀格式
  105. */
  106. public Builder setCompressFormat(Bitmap.CompressFormat compressFormat) {
  107. mCompressHelper.compressFormat = compressFormat;
  108. return this;
  109. }
  110.  
  111. /**
  112. * 设置Bitmap的参数
  113. */
  114. public Builder setBitmapConfig(Bitmap.Config bitmapConfig) {
  115. mCompressHelper.bitmapConfig = bitmapConfig;
  116. return this;
  117. }
  118.  
  119. /**
  120. * 设置压缩质量,建议80
  121. * @param quality 压缩质量,[0,100]
  122. */
  123. public Builder setQuality(int quality) {
  124. mCompressHelper.quality = quality;
  125. return this;
  126. }
  127.  
  128. /**
  129. * 设置目的存储路径
  130. * @param destinationDirectoryPath 目的路径
  131. */
  132. public Builder setDestinationDirectoryPath(String destinationDirectoryPath) {
  133. mCompressHelper.destinationDirectoryPath = destinationDirectoryPath;
  134. return this;
  135. }
  136.  
  137. /**
  138. * 设置文件前缀
  139. * @param prefix 前缀
  140. */
  141. public Builder setFileNamePrefix(String prefix) {
  142. mCompressHelper.fileNamePrefix = prefix;
  143. return this;
  144. }
  145.  
  146. /**
  147. * 设置文件名称
  148. * @param fileName 文件名
  149. */
  150. public Builder setFileName(String fileName) {
  151. mCompressHelper.fileName = fileName;
  152. return this;
  153. }
  154.  
  155. public CompressHelper build() {
  156. return mCompressHelper;
  157. }
  158. }
  159. }

使用

  1. File oldFile = CompressHelper.getDefault(getApplicationContext()).compressToFile(file);

自定义属性使用

  1. File newFile = new CompressHelper.Builder(this)
  2. .setMaxWidth(720) // 默认最大宽度为720
  3. .setMaxHeight(960) // 默认最大高度为960
  4. .setQuality(80) // 默认压缩质量为80
  5. .setFileName(yourFileName) // 文件名称
  6. .setCompressFormat(CompressFormat.JPEG) // 设置默认压缩为jpg格式
  7. .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
  8. Environment.DIRECTORY_PICTURES).getAbsolutePath())//路径
  9. .build()
  10. .compressToFile(oldFile);

该案例参考了:

android -------- 压缩图片文件工具类的更多相关文章

  1. android ImageUtils 图片处理工具类

    /** * 加入文字到图片.相似水印文字. * @param gContext * @param gResId * @param gText * @return */ public static Bi ...

  2. Android FileUtil(android文件工具类)

    android开发和Java开发差不了多少,也会有许多相同的功能.像本文提到的文件存储,在Java项目和android项目里面用到都是相同的.只是android开发的一些路径做了相应的处理. 下面就是 ...

  3. Android开源项目大全 - 工具类

    主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过依赖注入减少Vie ...

  4. Android经常使用的工具类

    主要介绍总结的Android开发中经常使用的工具类,大部分相同适用于Java. 眼下包含HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils. Pr ...

  5. 图片处理工具类 - ImageUtils.java

    纯JAVA实现的图片处理工具类,提供图片的裁剪.压缩.获取尺寸.制作圆角等方法. 源码如下:(点击下载 -ImageUtils.java .FolderUtils.java .commons-io-2 ...

  6. Java操作图片的工具类

    操作图片的工具类: import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.a ...

  7. 自动扫描FTP文件工具类 ScanFtp.java

    package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

  8. java图片处理工具类

    直接上代码: package com.zxd.tool; /** * Created by zhang on 14-3-1. * 图片的常用操作类 */ import java.awt.AlphaCo ...

  9. Android开发调试日志工具类[支持保存到SD卡]

    直接上代码: package com.example.callstatus; import java.io.File; import java.io.FileWriter; import java.i ...

随机推荐

  1. Interllij IDEA中启动web项目

    1.在IDEA中打开你的Web应用,点击一下绿色三角形左边的框框,然后在弹出框上选择Edit Configurations,会弹出一个配置面板. 2.在弹出的面板中我们点击Defaults,然后找到T ...

  2. 【托业】【新东方托业全真模拟】TEST07~08-----P5~6

    unless ---conj:barring(除非,不包括)perp+名词短语 be capable of doing 有能力做某事 qualified commensurate with 与……相应 ...

  3. 【托业】新托业全真题库---TEST1

    clearly indicate ——clearly可以修饰indicate(表明:暗示:指示) recently只用于现在完成时和过去完成时中 municipal gallery 市立美术馆 per ...

  4. python基础(14)-反射&类的内置函数

    反射 几个反射相关的函数可参考python基础(10)-匿名函数&内置函数中2.2.4反射相关 类的一些内置函数 __str__()&__repr__() 重写__str__()函数类 ...

  5. python基础之 初识函数&函数进阶

    函数基础部分 1.什么是函数? 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段.函数能提高应用的模块性,和代码的重复利用率. 2.定义函数 定义:def 关键词开头,空格之后接函数名 ...

  6. 新项目增加gradlew

    新项目,增加gradlew git clone http://git.inspur.com/iop/gradle.git git submodule update --init

  7. 赵丽颖固然漂亮,可这份Hadoop核心教程也不差呀

    阿里巴巴采用了 15 个节点组成的 Hadoop 集群,用于处理从数据库中导出的商业数据的排序和组合. Ebay 使用了 32 个节点组成的集群,包括 8 532 个计算核心以及 5.3 PB 的存储 ...

  8. CentOS 7 下使用yum安装MySQL5.7.20

    CentOS7默认数据库是mariadb, 但是 好多用的都是mysql ,但是CentOS7的yum源中默认好像是没有mysql的. 上一篇安装的是5.6的但是我想安装5.7的  yum安装是最简单 ...

  9. WinDBG相关

    WinDBG 技巧:如何生成Dump 文件(.dump 命令)   程序崩溃(crash)的时候, 为了以后能够调试分析问题, 可以使用WinDBG要把当时程序内存空间数据都保存下来,生成的文件称为d ...

  10. word之个人设置

    1.视图设置.五种视图中一般都是用“页面视图”.标尺和导航窗口都需要显示出来,方便操作,显示比例就用最真实的100%比例. 2.设置文件自动保存时间间隔和位置 3.word段落设置,不允许西文单词中间 ...