uCrop icon indicating copy to clipboard operation
uCrop copied to clipboard

How to use uCrop with new registerForActivityResult?

Open dodyac opened this issue 3 years ago • 7 comments

dodyac avatar Jun 04 '21 03:06 dodyac

#713 has some related discussion.

rmtheis avatar Jul 12 '21 14:07 rmtheis

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

garrygk avatar Jul 18 '21 17:07 garrygk

I am also looking forward to this fix

david-kariuki avatar Aug 05 '21 14:08 david-kariuki

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) `

muhammad-hamza-shahid avatar Aug 11 '21 07:08 muhammad-hamza-shahid

I created solution for launch UCrop Activity :

ActivityResultLauncher<Intent> 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;
}

chirag-deshwal avatar Oct 04 '21 08:10 chirag-deshwal

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 !

ousmane-diallo-sio avatar Nov 12 '22 20:11 ousmane-diallo-sio

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

icecoins avatar Feb 07 '23 12:02 icecoins