2X faster development
The ultimate Xamarin UI toolkit to boost your development speed.
By default, in SfImageEditor when we save image, the image will be getting saved in gallery. We can also able to change the image saving location.
Step 1: Create SfImageEditor sample with all necessary assemblies.
Please refer the below link to create a simple SfImageEditor sample along with the ways to configure it. https://help.syncfusion.com/xamarin/sfimageeditor/getting-started
Step 2: We can able to change the image saving location for all the xamarin platforms. For this, you need to implement a DependencyService which is implemented across all the platform specific projects.
For adding the DependencyService, first you need to add an interface in the PCL project as in the below code snippet:
public interface IDependency { string SelectDirectory(Stream stream); // Select Directory }
Now, you have to implement and register the DependencyService in the assembly based on platform specific. This can be done as follows:
Android
[assembly: Xamarin.Forms.Dependency(typeof(ImageEditor_GettingStarted.Droid.DependencyImplementation))] public class DependencyImplementation : IDependency { public void SelectDirectory(Stream stream) { activity = Xamarin.Forms.Forms.Context as MainActivity; activity.Intent = new Intent(); activity.Intent.SetAction(Intent.ActionOpenDocumentTree); activity.StartActivityForResult(activity.Intent, REQUEST_CODE_OPEN_DIRECTORY); activity.ActivityResult += (object sender, ActivityResultEventArgs e) => { FolderPath = e.Intent.Data; string DummyPath = FolderPath.Path; OriginalPath = DummyPath.Split(':')[1]; RealPath = "/storage/emulated/0/" + OriginalPath; Save(); }; str = stream; Toast.MakeText(activity, "Image has been saved into" + RealPath, ToastLength.Short).Show(); } }
iOS:
In iOS there is no possibilities to pick local directory for saving images.
Only we can save the image into particular directory.
[assembly: Xamarin.Forms.Dependency(typeof(ImageEditor_GettingStarted.iOS.DependencyImplementation))] public class DependencyImplementation : IDependency { public void SelectDirectory(Stream stream) { var bytearray = ReadFully(stream); var image = ImageFromBytes(bytearray); Save(image); } void Save(UIKit.UIImage image) { var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal); var directoryname = Path.Combine(documentsDirectory, "FolderName"); Directory.CreateDirectory(directoryname); string jpgFile = System.IO.Path.Combine(directoryname, "image.jpg"); Foundation.NSData imgData = image.AsJPEG(); Foundation.NSError err = null; if (imgData.Save(jpgFile, false, out err)) { Console.WriteLine("saved as" + jpgFile); } else { Console.WriteLine("not saved as " + jpgFile + "because" + err.LocalizedDescription); } var alert = new UIKit.UIAlertView("Saved Location", jpgFile, null, "OK"); alert.Show(); } }
UWP
[assembly: Xamarin.Forms.Dependency(typeof(ImageEditor_GettingStarted.UWP.DependencyImplementation))] public class DependencyImplementation : IDependency { public async void SelectDirectory(Stream stream) { this.stream = stream; IRandomAccessStream randomAccessStream = this.stream.AsRandomAccessStream(); var wbm = new WriteableBitmap(600, 800); await wbm.SetSourceAsync(randomAccessStream); string fileName = "image.jpg"; var folderPicker = new Windows.Storage.Pickers.FolderPicker(); folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop; folderPicker.FileTypeFilter.Add("*"); Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync(); string SelectedFolderPath = folder.Path; if (folder != null) { // Application now has read/write access to all contents in the picked folder // (including other sub-folder contents) Windows.Storage.AccessCache.StorageApplicationPermissions. FutureAccessList.AddOrReplace("PickedFolderToken", folder); StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); using (var storageStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, storageStream); var pixelStream = wbm.PixelBuffer.AsStream(); var pixels = new byte[pixelStream.Length]; await pixelStream.ReadAsync(pixels, 0, pixels.Length); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)wbm.PixelWidth, (uint)wbm.PixelHeight, 200, 200, pixels); await encoder.FlushAsync(); } } } }
Step3: Select Directory from DependencyService method call.
private void Imageeditor_ImageSaving(object sender, ImageSavingEventArgs args) { args.Cancel = true; stream = args.Stream; DependencyService.Get<IDependency>().SelectDirectory(stream); }
Sample link: |
2X faster development
The ultimate Xamarin UI toolkit to boost your development speed.
This page will automatically be redirected to the sign-in page in 10 seconds.