Yalantis / uCrop

Image Cropping Library for Android
https://yalantis.com/blog/introducing-ucrop-our-own-image-cropping-library-for-android/
11.86k stars 2.16k forks source link

Cropping works once, then next crop from same Activity doesn't work #223

Closed KaeMs closed 7 years ago

KaeMs commented 7 years ago

The current behavior is as in the title, when I send image either from Gallery and Camera, it works at first. Then for the subsequent cropping it doesn't work.

By doesn't work, I mean, Image Uri taken from UCrop.getOutput(result); gives the same Uri as the first image.

What I did is :

Uri compressedUri = Utilities.compressImage(this, uri);

Date date = calendar.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ddMMyyyyHHmmss", Locale.US);
String dateString = simpleDateFormat.format(date);
imageName = dateString + ".jpg";

UCrop uCrop = UCrop.of(compressedUri, Uri.fromFile(new File(getCacheDir(), imageName)));
UCrop.Options uCropOption = new UCrop.Options();
uCropOption.setToolbarColor(ContextCompat.getColor(this, R.color.miBlue));
uCropOption.setStatusBarColor(ContextCompat.getColor(this, R.color.miBlack));
uCropOption.setToolbarTitle(this.getString(R.string.cropTitle));
uCrop.withOptions(uCropOption);

uCrop.start(this);

imageRealPath shows it has the correct Uri (even if I use capturedImageUri.getPath)

Then at startCropActivity()

    Uri compressedUri = Utilities.compressImage(this, uri);

    Date date = calendar.getTime();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ddMMyyyyHHmmss", Locale.US);
    String dateString = simpleDateFormat.format(date);
    imageName = dateString + ".jpg";

    UCrop uCrop = UCrop.of(compressedUri, Uri.fromFile(new File(getCacheDir(), imageName)));
    UCrop.Options uCropOption = new UCrop.Options();
    uCropOption.setToolbarColor(ContextCompat.getColor(this, R.color.miBlue));
    uCropOption.setStatusBarColor(ContextCompat.getColor(this, R.color.miBlack));
    uCropOption.setToolbarTitle(this.getString(R.string.cropTitle));
    uCrop.withOptions(uCropOption);

    uCrop.start(this);

In handleCropResult()

    Uri resultUri = UCrop.getOutput(result);
    if (resultUri != null) {
        productImage.setVisibility(View.VISIBLE);

        Glide.with(this)
                .load(resultUri)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .skipMemoryCache(true)
                .bitmapTransform(new CenterCrop(this), new RoundedCornersTransformation(this, 10, 0))
                .into(productImage);
    //            imageRealPath = UtilityUriHelper.getPath(this, resultUri);
                imageChanged = true;
            } else {
                Toast.makeText(this, R.string.toast_cannot_retrieve_cropped_image, Toast.LENGTH_SHORT).show();
            }

Current version I am using is 2.2.0 of Ucrop, and it didn't work either in 2.1.2

gnuhel commented 7 years ago

@KaeMs you need to reset your imageview, it the same uri is set the the imageview does not refresh

TeeRawk commented 7 years ago

resolved