大佬们的博客
https://blog.csdn.net/qq_36043263/article/details/81707029
https://blog.csdn.net/u010356768/article/details/79026565
Matisse为我们提供了一个非常稳定了图片视频选择框架
github地址
依赖:
compile ‘com.zhihu.android:matisse:0.5.2-beta2’
implementation ‘com.github.bumptech.glide:glide:4.7.1’
annotationProcessor ‘com.github.bumptech.glide:compiler:4.7.1’
最基本的两个权限:
android.permission.READ_EXTERNAL_STORAGE
android.permission.WRITE_EXTERNAL_STORAGE
代码调用:
Matisse.from(this).choose(MimeType.ofImage(), false)
.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(1);
choose是选择的内容,countable()是否显示选中数量,maxSelectable()最大选择数,addFilter()添加一个过滤器,是在我们选择的类型上进一步过滤。gridExpectedSize()缩略图展示的大小,thumbnailScale(0.87f)缩略图的清晰程度(与内存占用有关)。imageEngine()是我们自定义加载图片框架。
Filter接口有两个方法,第一个方法返回需要过滤的数据类型,第二个方法决定是否过滤,过滤的话就return new IncapableCause(“宽度超过500px”); 填入过滤的原因即可。 在上述中我们过滤了宽度大于500的图片。
接收:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String path = Matisse.obtainPathResult(data).get(0);
if (uri != null) {
Glide.with(this)
.asBitmap() // some .jpeg files are actually gif
.load(uri)
.apply(new RequestOptions() {{
override(Target.SIZE_ORIGINAL);
}})
.into(imageView);
} else
Toast.makeText(this, "uri为null", Toast.LENGTH_SHORT).show();
}
}
}
1
loadThumbnail自然就是缩略图的加载方式,Matisse传入了相关参数直接使用即可。
loadImage就是加载大图模式。