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


本文出自 “绿兮上弦” 博客,转载请与作者联系!

更多相关文章

  1. android 自定义用相机拍照后的照片存储位置
  2. Android 7.0调用系统相机返回路径问题
  3. Android调用系统相机和图库
  4. Android 照相机
  5. Android OpenCV获取相机并拍(Android Studio)
  6. Android照相机竖屏研究引导
  7. 编程实现Android相机Camera设置
  8. Android中调用相机拍照之后裁剪得到的照片的源码
  9. android > 获取图片 从 本地 /相机

随机推荐

  1. Android(安卓)使用Intent传递数据的实现
  2. 基于NanoHttpd的Android视频服务器开发
  3. Android 获取android密钥哈希码(keytool -
  4. 苹果悄悄进入企业级市场,iOS起飞,Android折
  5. repo 和 git 管理源代码
  6. Android带进度条的下载图片示例(AsyncTask
  7. android不将apk包编译到系统里的方法
  8. java for android sqlite数据的操作:说实
  9. Android面试题(基础部分1)
  10. Android(安卓)第四天(晚上)