zoukankan      html  css  js  c++  java
  • Android 调用相机、打开相册、裁剪图片

        private ImageView iv_user_photo;
        private String fileName = "";
        private File tempFile;
        private int crop = 300;// 裁剪大小
        private static final int OPEN_CAMERA_CODE = 10;
        private static final int OPEN_GALLERY_CODE = 11;
        private static final int CROP_PHOTO_CODE = 12;
    
    
        private OnClickListener PopupWindowItemOnClick = new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                menuWindow.dismiss();
                switch (v.getId()) {
                // 拍照
                case R.id.btn_camera:
                    initFile();
                    openCamera();
                    break;
                // 相册
                case R.id.btn_gallery:
                    initFile();
                    openGallery();
                    break;
                default:
                    break;
                }
            }
        };
    
        public void initFile() {
            if(fileName.equals("")) {
                if(FileUtil.existSDCard()) {
                    String path = Environment.getExternalStorageDirectory() + File.separator + "JanuBookingOnline" + File.separator;
                    FileUtil.mkdir(path);
                    Logger.i("path:" + path);
                    fileName = path + "user_head_photo.jpg";
                    tempFile = new File(fileName);
                } else {
                    CommonUitl.toast(context, "请插入SD卡");
                }
            }
        }
    
        /**
         * 调用相机
         */
        public void openCamera() {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 打开相机
            intent.putExtra("output", Uri.fromFile(tempFile));
            startActivityForResult(intent, OPEN_CAMERA_CODE);
        }
    
        /**
         * 打开相册
         */
        public void openGallery() {
            Intent intent = new Intent(Intent.ACTION_PICK);// 打开相册
            intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");
            intent.putExtra("output", Uri.fromFile(tempFile));
            startActivityForResult(intent, OPEN_GALLERY_CODE);
        }
    
        /**
         * 裁剪图片
         * @param uri
         */
        public void cropPhoto(Uri uri) {
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(uri, "image/*");
            intent.putExtra("output", Uri.fromFile(tempFile));
            intent.putExtra("crop", true);
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            intent.putExtra("outputX", crop);
            intent.putExtra("outputY", crop);
            startActivityForResult(intent, CROP_PHOTO_CODE);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == 1)
                return;
    
            switch (requestCode) {
            case OPEN_CAMERA_CODE:
                cropPhoto(Uri.fromFile(tempFile));
                break;
            case OPEN_GALLERY_CODE:
                cropPhoto(data.getData());
                break;
            case CROP_PHOTO_CODE:
                try {
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 2;
                    Bitmap bitmap = BitmapFactory.decodeFile(fileName, options);
    
                    if (bitmap != null) {
                        iv_user_photo.setImageBitmap(bitmap);
                        CommonUitl.sharedPreferences(context, AppConstants.USER_PHOTO, fileName);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
            }
    
            super.onActivityResult(requestCode, resultCode, data);
        }

  • 相关阅读:
    powerdesigner 字段添加注释和默认值
    springboot集成enchance
    判断字段数据什么数据类型
    springboot打成jar包涉及到的linux命令
    springdatajpa添加完modle之后立即返回id
    阿里云上部署环境
    STS 启动之后, "Initializing Java Tooling" 一直卡住问题解决
    C#访问MongoDB数据
    MongoDB开发学习(1)开天辟地,经典入门
    Step by Step 設定 TFS 2012 Create Team Project 權限
  • 原文地址:https://www.cnblogs.com/chaoyaximo/p/3307538.html
Copyright © 2011-2022 走看看