Android CV系列 > 图片选择 严大的Ablum


1.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

2.

 //支持多选的安卓相册
    compile 'com.yanzhenjie:album:2.0.2'
//Glide
    compile 'com.github.bumptech.glide:glide:3.7.0'

3.

   private ImageView image;
    private int ACTIVITY_REQUEST_SELECT_PHOTO=12138;//随意

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        image.setOnClickListener(this);
    }

    private void initView() {
        image = (ImageView) findViewById(R.id.image);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image:
                Album.image(MainActivity.this) // 选择图片。
                        .multipleChoice()
                        .requestCode(ACTIVITY_REQUEST_SELECT_PHOTO)
                        .camera(true)//有照相机
                        .columnCount(3)//一行三个
                        .selectCount(1)//选择一个
                        .onResult(new Action<ArrayList<AlbumFile>>() {
                            @Override
                            public void onAction(int requestCode, @NonNull ArrayList<AlbumFile> result) {
                                AlbumFile albumFile = result.get(0);
                                String path = albumFile.getPath();
                                File file=new File(path);
//                                file = CompressHelper.getDefault(mContext).compressToFile(new File(path));
                                try {
                                    Glide.with(MainActivity.this)
                                            .load(file)
                                            .into(image);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        })
                        .onCancel(new Action<String>() {
                            @Override
                            public void onAction(int requestCode, @NonNull String result) {

                            }
                        })
                        .start();
                break;
            default:
                break;
        }
    }
}

3

 <ImageView
        android:scaleType="fitXY"
        android:id="@+id/image"
        android:src="@mipmap/ic_launcher"
        android:layout_width="100dp"
        android:layout_height="100dp"
        />

地址:https://github.com/yanzhenjie/Album

4.改进  图片压缩

 File  file = CompressHelper.getDefault(MainActivity.this).compressToFile(new File(path));

替换File就可以了

maven { url "https://jitpack.io" }
//图片压缩
compile 'com.github.nanchen2251:CompressHelper:1.0.5'

鲁班压缩 也是一句  但是星多

https://github.com/Curzibn/Luban

//图片压缩
implementation 'top.zibin:Luban:1.1.8'
 AlbumFile albumFile = result.get(0);
                                String path = albumFile.getPath();

                                Luban.with(MainActivity.this)
                                        .load(path)
                                        .ignoreBy(100)
                                        .filter(new CompressionPredicate() {
                                            @Override
                                            public boolean apply(String path) {
                                                return !(TextUtils.isEmpty(path) || path.toLowerCase().endsWith(".gif"));
                                            }
                                        })
                                        .setCompressListener(new OnCompressListener() {
                                            @Override
                                            public void onStart() {
                                                // TODO 压缩开始前调用,可以在方法内启动 loading UI
                                            }

                                            @Override
                                            public void onSuccess(File file) {
                                                // TODO 压缩成功后调用,返回压缩后的图片文件
                                                Glide.with(MainActivity.this)
                                                        .load(file)
                                                        .into(image);
                                            }

                                            @Override
                                            public void onError(Throwable e) {
                                                // TODO 当压缩过程出现问题时调用
                                            }
                                        }).launch();
                                try {

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

猜你喜欢

转载自blog.csdn.net/FlyPig_Vip/article/details/82760978
今日推荐