Android第三方開源庫:圖片壓縮
阿新 • • 發佈:2019-02-08
CompressHelper
原圖:
許可權:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
gradle
allprojects {
repositories {
maven { url 'https://jitpack.io' }
jcenter()
}
}
dependencies {
...
compile 'com.github.nanchen2251:CompressHelper:1.0.5'
}
預設壓縮
File newFile = compressHelper.compressToFile(oldFile);
自定義壓縮
File file = new CompressHelper.Builder(MainActivity.this)
.setMaxWidth(500)
.setMaxHeight(300)
.setFileName("newName")//自動新增字尾名.jpeg
.setQuality (50)
.setDestinationDirectoryPath(Environment.getExternalStorageDirectory().getAbsolutePath())//壓縮後的檔案儲存路徑
.build()
.compressToFile(oldFile);
問題
app module 的targetSdkVersion 22
以下,25壓縮後失敗,註釋掉
這個有時候報錯,有時候不報錯
setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath())
就可以正常壓縮了。
雙字尾名的問題
在壓縮後,出現了2個字尾名,排查發現
.setFileName(oldFile.getName())
.setCompressFormat(Bitmap.CompressFormat.JPEG)
造成的,file.getName()
獲取的是檔名是包含字尾名的,而我們又自定義了.JPEG
的字尾名,導致雙字尾名。
解決方法:
方法一:
不自定義壓縮,使用預設的壓縮,這樣只有1個字尾名
File newFile = CompressHelper.getDefault(context).compressToFile(oldFile);
方法二:
給壓縮檔案命名是,不使用原檔名,或去掉原檔名的字尾名。
int index = oldFile.getName().indexOf(".");
String newFileName = oldFile.getName().substring(0, index);
其他
ImageCompressUtil
TakePhoto
gradle
compile 'com.jph.takephoto:takephoto_library:4.0.3'
配置
CropOptions
是用於裁剪的配置類
CompressConfig
是用於壓縮的配置類
第一種:繼承的方式
- 繼承TakePhotoActivity、TakePhotoFragmentActivity、TakePhotoFragment三者之一。
- 通過getTakePhoto()獲取TakePhoto例項進行相關操作。
- 重寫以下方法獲取結果
void takeSuccess(TResult result);
void takeFail(TResult result,String msg);
void takeCancel();
第二種:組裝的方式
見README.md
簡單使用
開啟圖片選擇器
在Button的點選事件中,新增下面程式碼
TakePhoto takePhoto = getTakePhoto();
takePhoto.onEnableCompress(TakePhotoHelper.getCompressConfig(), true);
takePhoto.onPickMultipleWithCrop(6, TakePhotoHelper.getCropOptions());
// takePhoto.onPickMultiple(3);//不剪下
TakePhotoHelper
是對剪下和壓縮配置進行工具封裝
public class TakePhotoHelper {
private static CompressConfig config;
private static CropOptions cropOptions;
public static CompressConfig getCompressConfig() {
if (config == null) {
config = new CompressConfig.Builder()
.setMaxSize(102400)//壓縮到的最大大小,單位B,採用的是迴圈壓縮,呼叫enableQualityCompress(true)
.setMaxPixel(800)//長或寬不超過的最大畫素,單位px
.enableReserveRaw(true)//是否保留原檔案
// .enableQualityCompress(true)//是否啟動質量壓縮
.enablePixelCompress(true)//是否啟用畫素壓縮
.create();
}
return config;
}
public static CropOptions getCropOptions() {
if (cropOptions == null) {
cropOptions = new CropOptions.Builder()
.setAspectX(800)
.setAspectY(800)
.setWithOwnCrop(true)//是否剪下
.create();
}
return cropOptions;
}
}
重寫3個方法
@Override
public void takeFail(TResult result, String msg) {
super.takeFail(result, msg);
}
@Override
public void takeCancel() {
super.takeCancel();
}
@Override
public void takeSuccess(TResult result) {
super.takeSuccess(result);
ArrayList<TImage> images = result.getImages();
Map<String, File> map = new HashMap<>();
for (int i = 0; i < images.size(); i++) {
File file = new File(images.get(i).getCompressPath());
map.put(file.getName(), file);
Log.d(TAG, "file.getName()=" + file.getName());
}
Log.d(TAG, "檔案數量=" + map.size());
}
其中
images.get(i).getCompressPath()//壓縮圖片路徑
images.get(i).getOriginalPath()//原始圖片路徑