一个 图片选择器

抄袭别人的吧 知乎图片选择器Matisse使用

GitHub链接 https://github.com/zhihu/Matisse

使用心得
需申请存储权限
官方demo中没有申请相机权限,实际使用中发现不申请相机权限的话会崩溃
使用带有相机的图片选择器

权限

android.permission.READ_EXTERNAL_STORAGE  
android.permission.WRITE_EXTERNAL_STORAGE

//使用

  Matisse.from(this)
                .choose(MimeType.ofImage(), false)    // 选择 mime 的类型
                .countable(true)
                .capture(true)   //这个跟下面一块用  带相机   这俩不设置 会不带相机
                .captureStrategy(
                        new CaptureStrategy(true, "你的包名.fileprovider", "test"))
                .maxSelectable(9)   //这个是最多选择数量
                //.addFilter(new GifSizeFilter(800, 480, 30 * Filter.K * Filter.K))   //这个可以自定义也可以直接复制  也可以不用
                .gridExpectedSize(
                        getResources().getDimensionPixelSize(R.dimen.dp_100))  //展示图片的大小
                .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
                .thumbnailScale(0.5f)//// 缩略图的比例
                .imageEngine(new GlideEngine())  // 使用的图片加载引擎 
                .setOnSelectedListener((uriList, pathList) -> {
                    Log.e("onSelected", "onSelected: pathList=" + pathList);
                })
                .showSingleMediaType(true)
                .originalEnable(true)
                .maxOriginalSize(10)
                .autoHideToolbarOnSingleTap(true)
                .setOnCheckedListener(isChecked -> {
                    Log.e("isChecked", "onCheck: isChecked=" + isChecked);
                })
                .forResult(ConfigRequest.RequestCode.REQUEST_CODE_CHOOSE);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
        List<Uri> mSelected;= Matisse.obtainResult(data);
        Log.d("Matisse", "mSelected: " + mSelected);
        //若需设置在imageview中
        //imageView.setImageURI(mSelected.get(0));
    }
}

AndroidManifest.xml
android 7.0 网上有很多

        <!-- android 7.0 文件路径 -->
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.xiaohutech.hellohobby.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>

自定义 Filter

    private Filter mVideoFilter = new Filter() {
        @Override
        protected Set<MimeType> constraintTypes() {
            return new HashSet<MimeType>() {{
                add(MimeType.MP4);  //mime 类型 可写适合自己的自己 
            }};
        }
        @Override
        public IncapableCause filter(Context context, Item item) {
            Uri contentUri = item.getContentUri();
 			// 这里写你的逻辑 在选中的时候拦截 不让选中 例如:我这 限制视频文件大小
 			  if (fileByPath.length() > 30 * Filter.K * Filter.K) {
                return new IncapableCause("短视频,不要超过30M ");
            }
            return null;
        }
    };

好了 没啥好说的 网上资料一大堆 个人感觉挺好用的

猜你喜欢

转载自blog.csdn.net/luochenyxh/article/details/106869307