How do I make downloaded images fit to Image Button?

58 Views Asked by At

I am using the Android-Image-Cropper library. It seems to work fine with pictures taken with the camera itself, but when I select images from the gallery that were downloaded from the internet and crop them, the image does not completely cover the ImageButton.

Here is a picture of it not working (downloaded)

Here is one that is working (taken with camera)

Here is my code from the activity:

public void setProfilePicture(View view) {
CropImage.startPickImageActivity(this);
}

@Override
@SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // handle result of pick image chooser
    if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        Uri imageUri = CropImage.getPickImageResultUri(this, data);

        // For API >= 23 we need to check specifically that we have permissions to read external storage.
        if (CropImage.isReadExternalStoragePermissionsRequired(this, imageUri)) {
            // request permissions and handle the result in onRequestPermissionsResult()
            mCropImageUri = imageUri;
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},   CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE);
        } else {
            // no permissions required or already granted, can start crop image activity
            startCropImageActivity(imageUri);
        }
    }

    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
            Uri resultUri = result.getUri();
            profilepicture.setImageURI(resultUri);
        } else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception e = result.getError();
        }
    }
}

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    if (requestCode == CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE) {
        if (mCropImageUri != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // required permissions granted, start crop image activity
            startCropImageActivity(mCropImageUri);
        } else {
            Toast.makeText(this, "Cancelling, required permissions are not granted", Toast.LENGTH_LONG).show();
        }
    }
}

private void startCropImageActivity(Uri imageUri) {
    CropImage.activity(imageUri)
            .setGuidelines(CropImageView.Guidelines.ON)
            .start(this);

}

}

1

There are 1 best solutions below

0
On

Oh wait it figured it out.

I had to set android:scaleType="fitCenter" in my XML and

 .setAspectRatio(1, 1) 
 .setFixAspectRatio(true)

to my startCropImageActivity method.