Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ImagePickerService : IImagePickerService
- {
- public IImageSourceUtility ImageSourceUtility => new ImageSourceUtility();
- private void StartActivity()
- {
- var currentActivity = MainActivity.AppActivity;
- if (currentActivity != null)
- {
- var cropImageOptions = new CropImageOptions();
- cropImageOptions.MultiTouchEnabled = true;
- cropImageOptions.Guidelines = CropImageView.Guidelines.On;
- cropImageOptions.AspectRatioX = 1;
- cropImageOptions.AspectRatioY = 1;
- cropImageOptions.FixAspectRatio = true;
- cropImageOptions.Validate();
- var intent = new Intent();
- intent.SetClass(currentActivity, Class.FromType(typeof(ImagePickerOnResultActivity)));
- intent.PutExtra(CropImage.CropImageExtraSource, null as global::Android.Net.Uri); // Image Uri
- intent.PutExtra(CropImage.CropImageExtraOptions, cropImageOptions);
- currentActivity.StartActivity(intent);
- }
- else
- {
- throw new InvalidOperationException("Could not get current activity.");
- }
- }
- public Task<ImageSource> PickImageAsync()
- {
- StartActivity();
- return Task.Run(() =>
- {
- _waitHandle.WaitOne();
- var result = _pickAsyncResult;
- _pickAsyncResult = null;
- return result;
- });
- }
- private static ImageSource _pickAsyncResult;
- private static EventWaitHandle _waitHandle = new AutoResetEvent(false);
- // if crashes(in release mode after linking), see plugin desc for proguard config change required for this theme
- [Activity(Label = "CropImageActivity", Theme = "@style/Base.Theme.AppCompat")]
- //[Activity(Theme = "@style/Base.Theme.AppCompat")]
- public class ImagePickerOnResultActivity : Activity
- {
- protected override void OnCreate(Bundle savedInstanceState)
- {
- //base.OnCreate(savedInstanceState);
- // start activity
- var x = CropImage.Activity();
- x.SetGuidelines(CropImageView.Guidelines.On)
- .Start(this);
- }
- protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
- {
- if (requestCode == CropImage.CropImageActivityRequestCode)
- {
- CropImage.ActivityResult result = CropImage.GetActivityResult(data);
- if (resultCode == Result.Ok)
- {
- var croppedFileUri = new Uri(result.Uri.ToString());
- _pickAsyncResult = ImageSource.FromFile(croppedFileUri.LocalPath);
- _waitHandle.Set();
- }
- else if ((int)resultCode == CropImage.CropImageActivityResultErrorCode)
- {
- Java.Lang.Exception error = result.Error;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement