前言:这次带来的是使用RxPermissions+知乎图片选择器Matisse+图片压缩工具Luban实现图片选择的功能。
implementation 'com.zhihu.android:matisse:0.5.1'
implementation 'com.github.tbruyelle:rxpermissions:0.10.2'
implementation 'top.zibin:Luban:1.1.7'
1.首先在RxPermissions申请完权限后开启知乎图片选择器(注意:使用知乎开源框架Matisse需注意的地方)
RxPermissions rxPermissions=new RxPermissions(this);
rxPermissions.requestEachCombined(Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribe(new Consumer<Permission>() {
@Override
public void accept(Permission permission) throws Exception {
if (permission.granted) {
MyModule_PerfectInformationActivity.myModule_choiceImageEnum= MyModule_ChoiceImageEnum.PERFECTINFORMATION_PERSIONFRAGMENT_USERIMAGE;
Matisse.from(this)
.choose(MimeType.ofImage())
.countable(true)
.capture(true) //使用拍照功能
.captureStrategy(new CaptureStrategy(true, "com.example.yuyi.common_module.fileprovider")) //是否拍照功能,并设置拍照后图片的保存路径
.maxSelectable(1)
.restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
.thumbnailScale(0.8f)
.forResult(MatisseStateValue.REQUEST_CODE_CHOOSE);
} else if (permission.shouldShowRequestPermissionRationale) {
ToastUtils.showShort("您已拒绝权限申请");
} else {
ToastUtils.showShort("您已拒绝权限申请,请前往设置>应用管理>权限管理打开权限");
}
}
});
2.在onActivityResult方法中用Luban进行图片压缩处理
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == MatisseStateValue.REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
File fileByUri = FileUtil.getFileByUri(Matisse.obtainResult(data).get(0), this);
// 压缩文件
Luban.with(this)
.load(fileByUri)
.ignoreBy(100)
.filter(new CompressionPredicate() {
@Override
public boolean apply(String path) {
return !(TextUtils.isEmpty(path) || path.toLowerCase().endsWith(".gif"));
}
})
.setCompressListener(new OnCompressListener() {
@Override
public void onStart() {
Log.e("测试", "开始");
}
@Override
public void onSuccess(File file) {
//转换成功后的文件
}
@Override
public void onError(Throwable e) {
Log.e("测试", "失败");
}
}).launch();
}
}
这里需要我使用了一个工具类将Uri转为File
public class FileUtil {
public static File getFileByUri(Uri uri, Context context) {
String path = null;
if ("file".equals(uri.getScheme())) {
path = uri.getEncodedPath();
if (path != null) {
path = Uri.decode(path);
ContentResolver cr = context.getContentResolver();
StringBuffer buff = new StringBuffer();
buff.append("(").append(MediaStore.Images.ImageColumns.DATA).append("=").append("'" + path + "'").append(")");
Cursor cur = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA }, buff.toString(), null, null);
int index = 0;
int dataIdx = 0;
for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
index = cur.getColumnIndex(MediaStore.Images.ImageColumns._ID);
index = cur.getInt(index);
dataIdx = cur.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
path = cur.getString(dataIdx);
}
cur.close();
if (index == 0) {
} else {
Uri u = Uri.parse("content://media/external/images/media/" + index);
System.out.println("temp uri is :" + u);
}
}
if (path != null) {
return new File(path);
}
} else if ("content".equals(uri.getScheme())) {
// 4.2.2以后
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
path = cursor.getString(columnIndex);
}
cursor.close();
return new File(path);
} else {
//Log.i(TAG, "Uri Scheme:" + uri.getScheme());
}
return null;
}
}