美文网首页
【基础笔记】Android Studio拍照、选择相册(第三方框

【基础笔记】Android Studio拍照、选择相册(第三方框

作者: 究极无敌棒棒糖 | 来源:发表于2020-02-28 10:57 被阅读0次

    一、选择相册(FilePicker)

    1、导入依赖

    implementation 'cn.imlibo:FilePicker:v0.0.5_alpha'
    

    2、使用方法

    ①选择指定后缀文件

    FilePicker
                    .from(this)
                    .chooseForMimeType()//选择指定后缀文件
                    .setMaxCount(10)
                    .setFileTypes("png", "doc","apk", "mp3", "gif", "txt", "mp4", "zip")
                    .requestCode(REQUEST_CODE_CHOOSE)//确认码
                    .start();
    

    ②在图片选择器中选择图片或视频

    FilePicker
                    .from(this)
                    .chooseMedia()//图片选择器
                    .enabledCapture(true)
                    .setTheme(R.style.FilePicker_Dracula)
                    .requestCode(REQUEST_CODE_CHOOSE)
                    .start();
    

    ③接受返回的文件

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode != RESULT_OK) {
                return;
            }
            if (requestCode == REQUEST_CODE_CHOOSE) {
                ArrayList<EssFile> essFileList = data.getParcelableArrayListExtra(Const.EXTRA_RESULT_SELECTION);
                StringBuilder builder = new StringBuilder();
                for (EssFile file :
                        essFileList) {
                    builder.append(file.getMimeType()).append(" | ").append(file.getName()).append("\n\n");
                }
                textView.setText(builder.toString());//显示选取结果
               //setViewText(builder.toString(), pathName);通常上传的时候需要文件路径,新写一个方法保存文件路径
            }
        }
    

    3、Git地址

    FilePicker

    二、拍照(TakePhoto)

    ①、添加依赖

     repositories {
           //glide
            mavenCentral()
            google()
        }
    dependencies {
        .....
        implementation 'com.jph.takephoto:takephoto_library:4.0.3'
        //Glide图片显示
        implementation 'com.github.bumptech.glide:glide:4.9.0'
        annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
    }
    

    ②、Activity实现takephoto

    /**
     * 实现Takephoto,InvokeListener。重写多个方法,添加获取实例方法getPhoto,对图片进行压缩裁剪等操作。
     */
    public class GetPicture extends AppCompatActivity implements TakePhoto.TakeResultListener, InvokeListener {
    
        @BindView(R.id.iv_choose)
        ImageView ivChoose;
        @BindView(R.id.btn_photo)
        Button btnPhoto;
        @BindView(R.id.btn_picture)
        Button btnPicture;
        @BindView(R.id.btn_multiple_choice)
        Button btnMultipleChoice;
        @BindView(R.id.iv_choose2)
        ImageView ivChoose2;
        private TakePhoto takePhoto;
        private InvokeParam invokeParam;
        ArrayList<TImage> images;//返回的图片对象
        private int flag;//1-->打开相机(裁剪),2-->打开相册(裁剪),3-->打开相册(多选裁剪)
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            getTakePhoto().onCreate(savedInstanceState);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.get_picture);
            ButterKnife.bind(this);
        }
    
    
        /**
         * 获取TakePhoto实例
         *
         * @return
         */
        public TakePhoto getTakePhoto() {
            if (takePhoto == null) {
                takePhoto = (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this, this));
            }
            //设置压缩规则,最大500kb
            takePhoto.onEnableCompress(new CompressConfig.Builder().setMaxSize(500 * 1024).create(), true);
            return takePhoto;
        }
    
    
        /**
         * 拍照,选取照片等操作
         */
        private void takePhotoAndImage() {
            //图片保存路径
            File file = new File(getExternalCacheDir(), System.currentTimeMillis() + ".png");
            Uri uri = Uri.fromFile(file);
            //图片裁剪方式
            int size = Math.min(getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels);
            CropOptions cropOptions = new CropOptions.Builder().setOutputX(size).setOutputX(size).setWithOwnCrop(false).create();
            if (flag == 1) {
                //相机获取照片并剪裁
                takePhoto.onPickFromCaptureWithCrop(uri, cropOptions);
                //相机获取不剪裁
                //takePhoto.onPickFromCapture(uri);
            } else if (flag == 2) {
                //相册获取照片并剪裁
                takePhoto.onPickFromGalleryWithCrop(uri, cropOptions);
                //相册获取不剪裁
    //      takePhoto.onPickFromGallery();
            } else if (flag == 3) {
                //多选,并剪裁
    //            takePhoto.onPickMultipleWithCrop(3, cropOptions);
                //多选,不剪裁
                takePhoto.onPickMultiple(2);
            }
        }
    
        /**
         * 显示图片
         */
        private void showImg() {
            for (int i = 0, j = images.size(); i < j - 1; i += 2) {
                Glide.with(this).load(new File(images.get(i).getCompressPath())).into(ivChoose);
                Glide.with(this).load(new File(images.get(i + 1).getCompressPath())).into(ivChoose2);
            }
            if (images.size() % 2 == 1) {
                Glide.with(this).load(new File(images.get(images.size() - 1).getCompressPath())).into(ivChoose);
            }
    
        }
    
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            getTakePhoto().onActivityResult(requestCode, resultCode, data);
            super.onActivityResult(requestCode, resultCode, data);
        }
    
        @Override
        public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
            getTakePhoto().onSaveInstanceState(outState);
            super.onSaveInstanceState(outState, outPersistentState);
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            PermissionManager.TPermissionType type = PermissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
            PermissionManager.handlePermissionsResult(this, type, invokeParam, this);
        }
    
        @Override
        public void takeSuccess(TResult result) {
            images = result.getImages();
            showImg();
        }
    
        @Override
        public void takeFail(TResult result, String msg) {
            Toast.makeText(CallPhone.this,"获取图片失败",Toast.LENGTH_SHORT).show();
    
        }
    
        @Override
        public void takeCancel() {
            Toast.makeText(CallPhone.this, "用户取消操作", Toast.LENGTH_SHORT).show();
    
        }
    
        @Override
        public PermissionManager.TPermissionType invoke(InvokeParam invokeParam) {
            PermissionManager.TPermissionType type = PermissionManager.checkPermission(TContextWrap.of(this), invokeParam.getMethod());
            if (PermissionManager.TPermissionType.WAIT.equals(type)) {
                this.invokeParam = invokeParam;
            }
            return type;
        }
    
    
        @OnClick({R.id.btn_photo, R.id.btn_picture, R.id.btn_multiple_choice})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.btn_photo:
                    flag = 1;
                    takePhotoAndImage();
                    break;
                case R.id.btn_picture:
                    flag = 2;
                    takePhotoAndImage();
                    break;
                case R.id.btn_multiple_choice:
                    flag = 3;
                    takePhotoAndImage();
                    break;
            }
        }
    }
    

    ③实例图片

    选取前 选取时
    选取加载后

    ④Git地址

    TakePhoto

    相关文章

      网友评论

          本文标题:【基础笔记】Android Studio拍照、选择相册(第三方框

          本文链接:https://www.haomeiwen.com/subject/lrprwctx.html