android点击按钮打开相册,打开相机的代码

打开相册
首先在onclick方法中:

Intent intent = new Intent();
intent.setType("image/*");// 开启Pictures画面Type设定为image
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, REQUEST_CODE_SELECT_PHOTO);

其次,在onActivityResult的回调方法中:

if (resultCode == RESULT_OK) {
    Uri uri = Crop.getOutput(data);
    Bitmap bm;
    try {
        bm = ImageUtils.getZoomOutBitmap(this.getContentResolver(), uri, 750, 750);
        } catch (FileNotFoundException e) {
                    Toast.makeText(this, "图片找不到", Toast.LENGTH_SHORT).show();
                    return;
        }
//这里上传图片到服务器
                //HttpUtils.uploadCircleImg(rQueue, new BitmapUploadParam(uri.getPath() + ".jpg", bm, 70), this,
                        //CODE_EVAL_UPLOAD);
    } else if (resultCode == Crop.RESULT_ERROR) {
                Toast.makeText(this, Crop.getError(data).getMessage(), Toast.LENGTH_SHORT).show();
    }

打开相机
首先在onclick方法中:

private Uri tempUri;
if (tempUri == null)
tempUri = getTempUri();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
startActivityForResult(intent, REQUEST_CODE_SELECT_USEPHOTO);// 采用ForResult打开

其次,在onActivityResult的回调方法中:

```
 if (requestCode == REQUEST_CODE_SELECT_USEPHOTO && resultCode == RESULT_OK) {
            cropPhoto(tempUri);// 裁剪图片
        }

可以将拍照拍出来的照片进行裁剪,定义裁剪方法。

/**
     * 调用系统的裁剪
     * 
     * @param uri
     */
    public void cropPhoto(Uri uri) {
        if (tempUri == null)
            tempUri = getTempUri();
        Crop.of(uri, tempUri).asSquare().start(this, 3);
    }


protected Uri getTempUri() {
        File dir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        if (dir == null)
            dir = getFilesDir();
        if (!dir.exists())
            dir.mkdirs();
        File f = new File(dir, "heyqun-temp.jpg");
        return Uri.fromFile(f);
    }

你可能感兴趣的:(android技术)