android相关知识

知乎开源多选相册导入使用并添加删除按钮

2018-08-28  本文已影响0人  丿灬序曲

导入jar

compile 'com.zhihu.android:matisse:0.4.3'

调用多选相册

 Matisse.from(ReportMovieActivity.this)
                        .choose(MimeType.allOf()) // 选择 mime 的类型
                        .countable(true)
                        .maxSelectable(size) // 图片选择的最多数量
                        .gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))//图片显示的宽高大小
                        .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                        .thumbnailScale(0.85f) // 缩略图的比例
                        .imageEngine(new GlideEngine()) // 使用的图片加载引擎
                        .forResult(IMAGE); // 设置作为标记的请求码

根据返回的图片解析,并且添加删除按钮提供删除功能

 if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null) {
//            Uri selectedImage = data.getData();
            List<Uri> dataList = Matisse.obtainResult(data);
//            selectImageLl.addView(imageView);
//            imagePathList.add(imagePath);
            Log.v("dataList",""+dataList.toString());
            String[] filePathColumns = {MediaStore.Images.Media.DATA};
            for (int i = 0 ;i<dataList.size();i++){
                Cursor c = getContentResolver().query(dataList.get(i), filePathColumns, null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePathColumns[0]);
                String imagePath = c.getString(columnIndex);
                Log.v("imagePath", "" + imagePath);

                View view = LayoutInflater.from(this).inflate(R.layout.item_reprot_image,null);
                ImageView imageView = (ImageView) view.findViewById(R.id.iamge_iv);
                ImageView deleteImage = (ImageView) view.findViewById(R.id.report_delete_iv);
//                ImageView imageView = new ImageView(this);
//                imageView.setLayoutParams(new LinearLayout.LayoutParams(DensityUtil.dip2px(this,50),
//                        DensityUtil.dip2px(this,60)));

                File file = new File(imagePath);
                GlideLogic.glideLoadVideoImg324(this, file, imageView, DensityUtil.dip2px(this,50),
                        DensityUtil.dip2px(this,60));
                deleteImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        View view1 = (View) v.getTag();
                        selectImageLl.removeView(view1);
                        ImageView imageView1 = (ImageView) view1.findViewById(R.id.iamge_iv);
                        imagePathList.remove(imageView1);
                    }
                });
                deleteImage.setTag(view);//添加整体布局,方便点击按钮删除
                imageView.setTag(imagePath);//添加图片的本地路径,方便本地提交
                selectImageLl.addView(view);//添加view,显示图片
                imagePathList.add(imageView);//添加当前imageview对象,方便提交时候获取图片,根据图片再拿到tag获取本地地址
                c.close();
            }
上一篇 下一篇

猜你喜欢

热点阅读