Android 调用相机或图库,裁剪图片

public class ThirdActivity extends Activity{


    Button take,photo;
    ImageView image;
    private Uri photoUri = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_third);

        photo = (Button)findViewById(R.id.third_button1);
        take = (Button)findViewById(R.id.third_button2);
        image = (ImageView)findViewById(R.id.third_imageView1);

        photo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO 系统相册
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 调用android的图库
                startActivityForResult(i, 2);
            }
        });
        take.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO 系统相机
                Intent intent = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);// 调用android自带的照相机

                SimpleDateFormat timeStampFormat = new SimpleDateFormat(
                        "yyyy_MM_dd_HH_mm_ss");
                String filename = timeStampFormat
                        .format(new Date());
                **ContentValues values = new ContentValues();
                values.put(Media.TITLE, filename);**

                **photoUri = getContentResolver()
                        .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                values);**

                **intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        photoUri);**

                startActivityForResult(intent, 1);
            }
        });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case 1:
            //相机
            if (resultCode == RESULT_OK) {
                if (data != null && data.getData() != null) {
                    Uri uri = data.getData();
                    photoUri = uri;
                }
                startImageAction(photoUri, 200, 200, 3, true);
            }
            break;
        case 2:
            //相册
            if (resultCode == RESULT_OK) {
                if (data != null && data.getData() != null) {
                    Uri uri = data.getData();
                    photoUri = uri;
                    System.out.println(">>>>>>>>>>>>>>>>>>>>>>  " + photoUri.toString());
                }
                startImageAction(photoUri, 200, 200, 3, true);
            }
            break;
        case 3:
            if (data == null) {
                return;
            } else {
                Bundle extras = data.getExtras();
                if (extras != null) {
                    Bitmap bitmap = extras.getParcelable("data");
                    if (bitmap != null) {
                        image.setImageBitmap(bitmap);

                        if (bitmap != null && bitmap.isRecycled()) {
                            bitmap.recycle();
                        }
                    }
                }

            }
            break;
        case Activity.RESULT_CANCELED:
            break;

        }
    }

    private void startImageAction(Uri uri, int outputX, int outputY,
            int requestCode, boolean isCrop) {
        Intent intent = null;
        if (isCrop) {
            intent = new Intent("com.android.camera.action.CROP");
        } else {
            //intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        }
        //在这个Uri下请求什么类型的数据
        intent.setDataAndType(uri, "image/*");
        //裁剪照片的信号
        intent.putExtra("crop", "true");
        // 剪切的宽高比为1:2
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        //裁剪区的宽
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);
        //是否保留比例
        intent.putExtra("scale", true);
        //直接输出文件————改变原来的数据
//      intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        //是否返回数据
        intent.putExtra("return-data", true);
//      intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        //关闭人脸检测
        intent.putExtra("noFaceDetection", true);
        startActivityForResult(intent, requestCode);
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值