Matisse图片选择框架使用


build.gradle引入
implementation 'com.zhihu.android:matisse:0.5.2-beta4'
代码使用:
public void selectImage() {
Matisse.from(this)
.choose(MimeType.ofImage(), false)
.capture(true) // 使用相机,和 captureStrategy 一起使用
.captureStrategy(new CaptureStrategy(true, "com.cswl.tradersmake.fileprovider"))
.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 = context.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;
}
})
.restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
.thumbnailScale(0.87f)
.imageEngine(new GlideLoadEngine())
.forResult(AppConfig.INTRODUCE_IMG_REQUEST_CODE);
}

//文件路径问题
private String handlePathBeforeKitKat(Intent data) {
List<Uri> pathList = Matisse.obtainResult(data);
String path = pathList.get(0).getPath();
return path;
}

@TargetApi(19)
private String handlePathOnKitKat(Intent data) {
String path = null;
Uri uri = Matisse.obtainResult(data).get(0);
if (DocumentsContract.isDocumentUri(this, uri)) {
String docId = DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
String id = docId.split(":")[1];
String selection = MediaStore.Images.Media._ID + "=" + id;
path = getPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
path = getPath(contentUri, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
path = getPath(uri, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
path = uri.getPath();
}
return path;
}

private String getPath(Uri uri, String selection) {
String path = null;
Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
return path;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AppConfig.INTRODUCE_IMG_REQUEST_CODE && resultCode == RESULT_OK) {
String filePath;
if (Build.VERSION.SDK_INT >= 19) {
filePath = handlePathOnKitKat(data);
} else {
filePath = handlePathBeforeKitKat(data);
}
if (!TextUtils.isEmpty(filePath)) {
// File file = new File(filePath);
File file = BitmapUtils.compressBitmapToFile(this, filePath);
if (file == null) {
Log.e("TAG", "onActivityResult: file 压缩失败");
file = new File(filePath);
}
Log.e("TAG", "onActivityResult: " + filePath);
RequestBody body = RequestBody.create(MediaType.parse("image/png"), file);
MultipartBody.Part part = MultipartBody.Part.createFormData("file", FileUtils.getFileNameFromPath(filePath) + ".png", body);
uploadImg(part);
}
}
}
posted @ 2021-03-15 16:21  مجنونة  阅读(315)  评论(0)    收藏  举报