Open dodyac opened 3 years ago
thank you @rmtheis , but it's still a little unclear how uCrop would work with registerForActivityResult since uCrop needs the requestCode.
I am also looking forward to this fix
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) `
I created solution for launch UCrop Activity :
ActivityResultLauncher
@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;
}
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 !
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
713 has some related discussion.