调用相机相册

@OnClick(R.id.publish_img_picture)
    public void onImgClick() {  //添加照片
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("图片");
        builder.setMessage("上传的途径");
        builder.setCancelable(true);

        builder.setPositiveButton("相机", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(path)));
                startActivityForResult(intent,1000);

            }
        });
        builder.setNegativeButton("相册", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent=new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent,2000);

            }
        });
        builder.show();

    }
//返回重写

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2000 & resultCode == RESULT_OK) {
            Uri uri = data.getData();
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(uri, "image/*");
            intent.putExtra("crop", true);
            intent.putExtra("aspactX", 1);
            intent.putExtra("aspactY", 1);
            intent.putExtra("outputX", 70);
            intent.putExtra("outputY", 70);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, 3000);
        } else if (requestCode == 1000 & resultCode == RESULT_OK) {

            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(Uri.fromFile(new File(path)), "image/*");
            intent.putExtra("crop", true);
            intent.putExtra("aspactX", 1);
            intent.putExtra("aspactY", 1);
            intent.putExtra("outputX", 70);
            intent.putExtra("outputY", 70);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, 3000);
        }
        
        if (requestCode == 3000 & resultCode == RESULT_OK) {

            Bitmap bitmap = data.getParcelableExtra("data");
            mImageView.setImageBitmap(bitmap);

        }
    }

 

你可能感兴趣的:(调用相机相册)