本文主要讲Luban对图片的压缩。Matisse的使用见本人的另篇文章,安卓图片选择框架Matisse的使用。
luban: https://github.com/Curzibn/Luban
需要先添加依赖
implementation 'top.zibin:Luban:1.1.8'
在通过Matisse完成图片的选择后,获取到的Uri传入luban,luban无法找到图片,所以无法进行压缩
List<Uri> pathList = Matisse.obtainResult(data);
因此通过Uri获取图片的路径后,再进行压缩
以下方式仅在安卓6.0验证过,其他版本不一定可行,请自行研究
/**
* 通过Uri获取文件路径
* @param pUri
* @return
*/
public String getPathByUri(Uri pUri)
{
// pUri.getPath()
// 拍照后输出: /mq_external_cache/storage/emulated/0/Pictures/JPEG_20190326_225011.jpg
// 选择照片后的输出: /external/images/media/52325
String _Path = pUri.getPath();
if (_Path.endsWith(".jpg"))
{
System.out.println("path-->" + subPath(_Path));
return subPath(_Path);
}
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(pUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
return picturePath;
}
private String subPath(String pPath)
{
String[] array = pPath.split("/");
return pPath.substring(array[1].length() + 1, pPath.length());
}
配合安卓图片选择框架Matisse的使用后,完整的MainActivity
代码如下
package com.jsf.piccompresstest;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.filter.Filter;
import com.zhihu.matisse.internal.entity.CaptureStrategy;
import com.zhihu.matisse.internal.entity.IncapableCause;
import com.zhihu.matisse.internal.entity.Item;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import io.reactivex.functions.Consumer;
import top.zibin.luban.CompressionPredicate;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;
/**
* 参考 https://blog.csdn.net/qq_36043263/article/details/81707029
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ImageView mView;
private final int REQUEST_CODE_CHOOSE_PHOTO_ALBUM = 1;
public static final String SD_APP_DIR_NAME = "TestDir"; //存储程序在外部SD卡上的根目录的名字
public static final String PHOTO_DIR_NAME = "photo"; //存储照片在根目录下的文件夹名字
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_select_pic).setOnClickListener(this);
mView = findViewById(R.id.iv_photo);
initPermission();
}
private void initPermission()
{
RxPermissions rxPermissions=new RxPermissions(this);
rxPermissions.request(
Manifest.permission.CAMERA
,Manifest.permission.WRITE_EXTERNAL_STORAGE
,Manifest.permission.READ_EXTERNAL_STORAGE
).subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
if (aBoolean){
//申请的权限全部允许
// Toast.makeText(MainActivity.this, "允许了权限!", Toast.LENGTH_SHORT).show();
}else{
//只要有一个权限被拒绝,就会执行
// Toast.makeText(MainActivity.this, "未授权权限,部分功能不能使用", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onClick(View view) {
switch (view.getId())
{
case R.id.btn_select_pic:
selectPic();
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_CHOOSE_PHOTO_ALBUM && resultCode == RESULT_OK)
{
//图片路径 同样视频地址也是这个 根据requestCode
List<Uri> pathList = Matisse.obtainResult(data);
List<String> _List = new ArrayList<>();
for (Uri _Uri : pathList)
{
String _Path = getPathByUri(_Uri);
File _File = new File(_Path);
System.out.println("压缩前图片大小->" + _File.length() / 1024 + "k");
_List.add(_Path);
}
compress(_List);
}
}
private void compress(List<String> list)
{
String _Path = getImagesPath();
System.out.println("_Path->" + _Path);
Luban.with(this)
.load(list)
.ignoreBy(100)
.setTargetDir(_Path)
.filter(new CompressionPredicate() {
@Override
public boolean apply(String path) {
return !(TextUtils.isEmpty(path) || path.toLowerCase().endsWith(".gif"));
}
})
.setCompressListener(new OnCompressListener() {
@Override
public void onStart() {
// TODO 压缩开始前调用,可以在方法内启动 loading UI
Toast.makeText(MainActivity.this, "I'm start", Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(File file) {
// TODO 压缩成功后调用,返回压缩后的图片文件
Glide.with(MainActivity.this).load(file).into(mView);
System.out.println("压缩后图片大小->" + file.length() / 1024 + "k");
System.out.println("getAbsolutePath->" + file.getAbsolutePath());
}
@Override
public void onError(Throwable e) {
// TODO 当压缩过程出现问题时调用
e.printStackTrace();
}
}).launch();
}
void selectPic()
{
Matisse.from(this)
.choose(MimeType.ofImage(), false)
.capture(true) // 使用相机,和 captureStrategy 一起使用
.captureStrategy(new CaptureStrategy(true, "com.jsf.piccompresstest123"))
// R.style.Matisse_Zhihu (light mode)
// R.style.Matisse_Dracula (dark mode)
.theme(R.style.Matisse_Dracula)
.countable(true)
.maxSelectable(1)
.addFilter(new Filter() {
@Override
protected Set<MimeType> constraintTypes() {
return new HashSet<MimeType>() {{
add(MimeType.PNG);
}};
}
@Override
public IncapableCause filter(Context context, Item item) {
try {
InputStream inputStream = getContentResolver().openInputStream(item.getContentUri());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);
int width = options.outWidth;
int height = options.outHeight;
// if (width >= 500)
// return new IncapableCause("宽度超过500px");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
})
// .gridExpectedSize((int) getResources().getDimension(R.dimen.imageSelectDimen))
.restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
.thumbnailScale(0.87f)
.imageEngine(new GlideLoadEngine())
.forResult(REQUEST_CODE_CHOOSE_PHOTO_ALBUM);
}
/**
* 通过Uri获取文件路径
* @param pUri
* @return
*/
public String getPathByUri(Uri pUri)
{
// pUri.getPath()
// 拍照后输出: /mq_external_cache/storage/emulated/0/Pictures/JPEG_20190326_225011.jpg
// 选择照片后的输出: /external/images/media/52325
String _Path = pUri.getPath();
if (_Path.endsWith(".jpg"))
{
System.out.println("path-->" + subPath(_Path));
return subPath(_Path);
}
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(pUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
return picturePath;
}
private String subPath(String pPath)
{
String[] array = pPath.split("/");
return pPath.substring(array[1].length() + 1, pPath.length());
}
private String getImagesPath()
{
return createPathIfNotExist("/" + SD_APP_DIR_NAME + "/" + PHOTO_DIR_NAME);
}
private String createPathIfNotExist(String pPath)
{
boolean sdExist = android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState());
if (!sdExist) {
Log.e("path", "SD卡不存在");
return null;
}
String _AbsolutePath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + pPath;
System.out.println("dbDir->" + _AbsolutePath);
File dirFile = new File(_AbsolutePath);
if (!dirFile.exists()) {
if (!dirFile.mkdirs())
{
Log.e("path", "文件夹创建失败");
return null;
}
}
return _AbsolutePath;
}
}