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);
        }

  • 相关阅读:
    MySQL命令 导出 数据和结构
    Maven web 项目工程的建立
    Maven的配置以及Eclipse的设置
    项目管理工具Maven的安装
    centos7 安装 redis
    Java + 腾讯邮箱 SSL加密问题 重要通知
    centos7 上配置Javaweb---MySQL的安装与配置、乱码解决
    关于阿里云Centos服务器搭建Java网站不能访问的问题
    浏览器使用经验
    Linux常用命令大全
  • 原文地址:https://www.cnblogs.com/chaoyaximo/p/3307538.html
Copyright © 2011-2022 走看看