Matisse,是一款由 知乎开源的媒体选择器。清新,可选择图片或视频,可自定义图片Loader,可自定义主题,感觉是当前最Fashion。
build.gradle:
compile 'com.zhihu.android:matisse:0.4.3'
需要权限
AndroidManifest.xml
android.permission.READ_EXTERNAL_STORAGE
android.permission.WRITE_EXTERNAL_STORAGE
若是Android6.0以上,需动态申请权限哟(推荐用RxPermissions)
源码中
Matisse.from(MainActivity.this)
.choose(MimeType.allOf())
.countable(true)
.maxSelectable(9)
.capture(true)
.captureStrategy(new CaptureStrategy(true, "com.szt.myapplicationee.fileprovider"))
//.addFilter(new GifSizeFilter(320, 320, 5 * Filter.K * Filter.K))
.gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
.restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
.thumbnailScale(0.85f)
.imageEngine(new GlideEngine())
.forResult(REQUEST_CODE_CHOOSE);
List<Uri> mSelected;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
mSelected = Matisse.obtainResult(data);
Log.d("Matisse", "mSelected: " + mSelected);
//若需设置在imageview中
//imageView.setImageURI(mSelected.get(0));
}
}
注意
1.拍照
若需拍照也显示在选择器中,需加上
.capture(true)
.captureStrategy(
new CaptureStrategy(true, "com.szt.myapplicationee.fileprovider"))
然后需要配置fileprovider:
在AndroidManifest.xml中加:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.szt.myapplicationee.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths_public"></meta-data>
</provider>
在values文件夹下新建xml文件夹
新建file_paths_public.xml文件
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="my_images"
path="Pictures"/>
</paths>
2.展示相册图片大小:
R.dimen.grid_expected_size的设置
<dimen name="grid_expected_size">120dp</dimen>
3.GifSizeFilter
GifSizeFilter在新版中貌似没了,可以不用GifSizeFilter,一定要用可以去demo中拷贝
4.不具备裁剪功能
不具备裁剪功能!不具备裁剪功能!
网友评论