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

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

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

效果图

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

工具类代码

public class CompressHelper {
private static volatile CompressHelper INSTANCE; private Context context;
/**
* 最大宽度,默认为720
*/
private float maxWidth = 720.0f;
/**
* 最大高度,默认为960
*/
private float maxHeight = 960.0f;
/**
* 默认压缩后的方式为JPEG
*/
private Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG; /**
* 默认的图片处理方式是ARGB_8888
*/
private Bitmap.Config bitmapConfig = Bitmap.Config.ARGB_8888;
/**
* 默认压缩质量为80
*/
private int quality = 80;
/**
* 存储路径
*/
private String destinationDirectoryPath;
/**
* 文件名前缀
*/
private String fileNamePrefix;
/**
* 文件名
*/
private String fileName; public static CompressHelper getDefault(Context context) {
if (INSTANCE == null) {
synchronized (CompressHelper.class) {
if (INSTANCE == null) {
INSTANCE = new CompressHelper(context);
}
}
}
return INSTANCE;
} private CompressHelper(Context context) {
this.context = context;
destinationDirectoryPath = context.getCacheDir().getPath() + File.pathSeparator + FileUtil.FILES_PATH;
} /**
* 压缩成文件
* @param file 原始文件
* @return 压缩后的文件
*/
public File compressToFile(File file) {
return BitmapUtil.compressImage(context, Uri.fromFile(file), maxWidth, maxHeight,
compressFormat, bitmapConfig, quality, destinationDirectoryPath,
fileNamePrefix, fileName);
} /**
* 压缩为Bitmap
* @param file 原始文件
* @return 压缩后的Bitmap
*/
public Bitmap compressToBitmap(File file) {
return BitmapUtil.getScaledBitmap(context, Uri.fromFile(file), maxWidth, maxHeight, bitmapConfig);
} /**
* 采用建造者模式,设置Builder
*/
public static class Builder {
private CompressHelper mCompressHelper; public Builder(Context context) {
mCompressHelper = new CompressHelper(context);
} /**
* 设置图片最大宽度
* @param maxWidth 最大宽度
*/
public Builder setMaxWidth(float maxWidth) {
mCompressHelper.maxWidth = maxWidth;
return this;
} /**
* 设置图片最大高度
* @param maxHeight 最大高度
*/
public Builder setMaxHeight(float maxHeight) {
mCompressHelper.maxHeight = maxHeight;
return this;
} /**
* 设置压缩的后缀格式
*/
public Builder setCompressFormat(Bitmap.CompressFormat compressFormat) {
mCompressHelper.compressFormat = compressFormat;
return this;
} /**
* 设置Bitmap的参数
*/
public Builder setBitmapConfig(Bitmap.Config bitmapConfig) {
mCompressHelper.bitmapConfig = bitmapConfig;
return this;
} /**
* 设置压缩质量,建议80
* @param quality 压缩质量,[0,100]
*/
public Builder setQuality(int quality) {
mCompressHelper.quality = quality;
return this;
} /**
* 设置目的存储路径
* @param destinationDirectoryPath 目的路径
*/
public Builder setDestinationDirectoryPath(String destinationDirectoryPath) {
mCompressHelper.destinationDirectoryPath = destinationDirectoryPath;
return this;
} /**
* 设置文件前缀
* @param prefix 前缀
*/
public Builder setFileNamePrefix(String prefix) {
mCompressHelper.fileNamePrefix = prefix;
return this;
} /**
* 设置文件名称
* @param fileName 文件名
*/
public Builder setFileName(String fileName) {
mCompressHelper.fileName = fileName;
return this;
} public CompressHelper build() {
return mCompressHelper;
}
}
}

使用

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

自定义属性使用

File newFile = new CompressHelper.Builder(this)
.setMaxWidth(720) // 默认最大宽度为720
.setMaxHeight(960) // 默认最大高度为960
.setQuality(80) // 默认压缩质量为80
.setFileName(yourFileName) // 文件名称
.setCompressFormat(CompressFormat.JPEG) // 设置默认压缩为jpg格式
.setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getAbsolutePath())//路径
.build()
.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. PHP计算年龄

    <?php $birthday = strtotime('1992-10-03'); $time = time();//2019-03-14; function datediffage($bir ...

  2. python摸爬滚打之day20--多继承,MRO和C3算法

    1.新式类和经典类 在python2.2之前, 基类如果不写(), 则表示为经典类; 在python2.2之后, 经典类不复存在, 只存在新式类. 如果基类谁都不继承的话, 则默认继承object. ...

  3. 浅谈AC自动机

    写在前面:从10月23日开始写这篇博文,离NOIP2018只有十多天了.坚持不停课的倔强蒟蒻(我)尽量每天挤时间多搞一搞信竞(然而还要准备期中考试).NOIP争取考一个好成绩吧. 一.简介 AC自动机 ...

  4. Bootstrap modal模态框关闭时,combobox input下拉框仍然保留在页面上

    问题描述: 当点击模态框的关闭按钮时,下拉框中的内容没有消失,而是移动到了页面左上角 分析:这个问题的定位在于是用的哪种模态框,bootstrap和easyui都可以实现模态框,但是两个方法实现的模态 ...

  5. C++提供的四种新式转换--const_cast dynamic_cast reinterpret_cast static_cast

    关于强制类型转换的问题,许多书都讨论过,写的最具体的是C++之父的<C++的设计和演化>. 最好的解决方法就是不要使用C风格的强制类型转换,而是使用标准C++的类型转换符:static_c ...

  6. 005-mac下Java开发工具安装,idea,maven,git,node

    1.idea 官网下载:https://www.jetbrains.com/idea/download/#section=mac 1.1.双击安装即可. 1.2.打开程序,激活,网络搜索lanyu 1 ...

  7. JavaScript事件起泡与捕获

    // 向 <div> 元素添加事件句柄 document.getElementById("myDIV").addEventListener("mousemov ...

  8. oracle 新建用户后赋予的权限语句

    grant create session,resource to itsys; grant create table to itsys;grant resource to itsys;grant cr ...

  9. vue中使用scss

    之前项目里我一般是使用less的,朋友问到如何引入scss,于是我就简单的跑了一下,以下主要供自己学习,如有更好的方法可以一起交流讨论一下 第一步,安装依赖 cnpm install node-sas ...

  10. 今天整理了一下Winform用的UI插件信息

    平时主要用了一下几个比较好的UI控件: 1:IrisSkin2 皮肤插件.这是一款与编程开发相关的素材资源,主要是提供一些采用IrisSkin2.dll控件进行软件窗口换肤的素材文件,包括一些GIF图 ...