我如何从用户那里获得图像? api使用:图像裁剪器,circleimageview

时间:2019-02-24 08:17:05

标签: java android

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(resultCode,requestCode,data);

    if (requestCode == 12 && resultCode == RESULT_OK && data!= null) ;
    {
        CropImage.activity()
                .setCropShape(CropImageView.CropShape.OVAL)

                .start(this);

    }
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
          resultUri=result.getUri();
          circleImageView.setImageURI(resultUri);


        }
        else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
        {
            Exception error = result.getError();


        }
    }


}

}

1 个答案:

答案 0 :(得分:0)

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Log.d("APP_DEBUG", String.valueOf(requestCode));

    try {
        // When an Image is picked
        if (requestCode == RESULT_SELECT_IMG && resultCode == Activity.RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();

            CropImage.activity(selectedImage)
                    .setGuidelines(CropImageView.Guidelines.ON)
                    .setMinCropResultSize(800, 800)
                    .setMaxCropResultSize(1000, 1000)
                    .start(this);
        }
        // when image is cropped
        else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            Log.d("APP_DEBUG",result.toString());
            if (resultCode == Activity.RESULT_OK) {
                resultUri = result.getUri();
                Log.d("APP_DEBUG",resultUri.toString());
                Bitmap bitmap =  MediaStore.Images.Media.getBitmap(this.getContentResolver(), resultUri);
                circleImageView.setImageBitmap(bitmap);

            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Exception error = result.getError();
            }
        }
        else {
            Toast.makeText(this, "You haven't picked Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong"+e.getMessage(), Toast.LENGTH_LONG)
                .show();
    }