Yalantis / uCrop

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

How to use uCrop with new registerForActivityResult? #765

Open dodyac opened 3 years ago

rmtheis commented 3 years ago

713 has some related discussion.

garrygk commented 3 years ago

thank you @rmtheis , but it's still a little unclear how uCrop would work with registerForActivityResult since uCrop needs the requestCode.

david-kariuki commented 3 years ago

I am also looking forward to this fix

muhammad-hamza-shahid commented 3 years ago

This is the simple example to use in Kotlin:

    `
    private const val RESULT_OK = -1
    private const val RESULT_CANCEL = 0

    var mGetContent = registerForActivityResult(
    StartActivityForResult()
) { result: ActivityResult ->
    if (result.resultCode == RESULT_OK) {
        assert(result.data != null)
        val resultUri = UCrop.getOutput(result.data!!)
        Picasso.get().load(resultUri)
            .into(binding.civ)
        if (resultUri != null) {
            imageUri = resultUri
        }
    } else if (result.resultCode == RESULT_CANCEL) {

        arguments?.let { argumentValue ->
            Picasso.get().load(
                Uri.parse(
                    CardDisplayFragmentArgs.fromBundle(
                        argumentValue
                    ).cardLink
                )
            )
                .into(binding.civ)

            imageUri = Uri.parse(
                CardDisplayFragmentArgs.fromBundle(
                    argumentValue
                ).cardLink
            )
        }
    }
}

val intent = UCrop.of(sourceUri, destinationUri)
        .getIntent(requireActivity())
    mGetContent.launch(intent) `
chirag-deshwal commented 3 years ago

I created solution for launch UCrop Activity :

ActivityResultLauncher uCropActivityResult;

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_parent_layout);

    uCropActivityResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult() , result -> {
            if (result.getData() != null && result.getResultCode() == RESULT_OK) {

            Uri cropedImage = UCrop.getOutput(result.getData());
        // Do Something  
    }

}

}

uCropActivityResult.launch(getUcropIntent(MyActivity.this, mediaScannerUri, Uri.fromFile(new File(getCacheDir(), fileName)), getUcropOptions()));

public static Intent getUcropIntent(Context context , Uri mediaScannerUri , Uri destination , UCrop.Options options ) { Intent intent = new Intent(); Bundle uCropBundle = options.getOptionBundle();

    // Src Uri
    uCropBundle.putParcelable(UCrop.EXTRA_INPUT_URI, mediaScannerUri);
    // Destination as Ucrop Normally save
    uCropBundle.putParcelable(UCrop.EXTRA_OUTPUT_URI, destination);

    intent.putExtras(options.getOptionBundle());
    intent.setClass(context , UCropActivity.class);
    return intent;
} 

public UCrop.Options getUcropOptions() { UCrop.Options options = new UCrop.Options(); options.setCompressionQuality(100); options.setCompressionFormat(Bitmap.CompressFormat.PNG); options.setToolbarTitle(getString(R.string.ucrop_tittle));

    //Change to clear view effect bitmap
    options.setRootViewBackgroundColor(Color.parseColor("#E6E6E6"));
    options.setLogoColor(Color.parseColor("#E6E6E6"));

    options.setFreeStyleCropEnabled(true);
    options.setToolbarColor(ContextCompat.getColor(this, R.color.white));
    options.setStatusBarColor(ContextCompat.getColor(this, R.color.eee));
    return options;
}
ousmane-diallo-sio commented 1 year ago

This is the simple example to use in Kotlin:

    `
    private const val RESULT_OK = -1
    private const val RESULT_CANCEL = 0

    var mGetContent = registerForActivityResult(
    StartActivityForResult()
) { result: ActivityResult ->
    if (result.resultCode == RESULT_OK) {
        assert(result.data != null)
        val resultUri = UCrop.getOutput(result.data!!)
        Picasso.get().load(resultUri)
            .into(binding.civ)
        if (resultUri != null) {
            imageUri = resultUri
        }
    } else if (result.resultCode == RESULT_CANCEL) {

        arguments?.let { argumentValue ->
            Picasso.get().load(
                Uri.parse(
                    CardDisplayFragmentArgs.fromBundle(
                        argumentValue
                    ).cardLink
                )
            )
                .into(binding.civ)

            imageUri = Uri.parse(
                CardDisplayFragmentArgs.fromBundle(
                    argumentValue
                ).cardLink
            )
        }
    }
}

val intent = UCrop.of(sourceUri, destinationUri)
        .getIntent(requireActivity())
    mGetContent.launch(intent) `

Thank you, this method works !

icecoins commented 1 year ago

I created solution for launch UCrop Activity :

ActivityResultLauncher uCropActivityResult;

@OverRide protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_parent_layout);

    uCropActivityResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult() , result -> {
            if (result.getData() != null && result.getResultCode() == RESULT_OK) {

            Uri cropedImage = UCrop.getOutput(result.getData());
        // Do Something  
    }

}

}

uCropActivityResult.launch(getUcropIntent(MyActivity.this, mediaScannerUri, Uri.fromFile(new File(getCacheDir(), fileName)), getUcropOptions()));

public static Intent getUcropIntent(Context context , Uri mediaScannerUri , Uri destination , UCrop.Options options ) { Intent intent = new Intent(); Bundle uCropBundle = options.getOptionBundle();

    // Src Uri
    uCropBundle.putParcelable(UCrop.EXTRA_INPUT_URI, mediaScannerUri);
    // Destination as Ucrop Normally save
    uCropBundle.putParcelable(UCrop.EXTRA_OUTPUT_URI, destination);

    intent.putExtras(options.getOptionBundle());
    intent.setClass(context , UCropActivity.class);
    return intent;
} 

public UCrop.Options getUcropOptions() { UCrop.Options options = new UCrop.Options(); options.setCompressionQuality(100); options.setCompressionFormat(Bitmap.CompressFormat.PNG); options.setToolbarTitle(getString(R.string.ucrop_tittle));

    //Change to clear view effect bitmap
    options.setRootViewBackgroundColor(Color.parseColor("#E6E6E6"));
    options.setLogoColor(Color.parseColor("#E6E6E6"));

    options.setFreeStyleCropEnabled(true);
    options.setToolbarColor(ContextCompat.getColor(this, R.color.white));
    options.setStatusBarColor(ContextCompat.getColor(this, R.color.eee));
    return options;
}

It works ! Thanks bro