We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to retrieve image from saved location?

Platform: Xamarin.Forms |
Control: SfImageEditor

By default, in Image editor when we save image, the image will be getting saved in gallery. We can also able to retrieve and display the image which is stored in the location. ImageSavedEventArgs used to get the saved image location as its argument. Please follow the following steps to retrieve image from saved location.

 

Step 1: Create image editor 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: To retrieve image in 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
{
    void RetriveImageFromLocation(string location,Image image);
  }

 

 

Now, you must implement and register the DependencyService in the assembly based on platform specific. This can be done as follows:

Android

 

public class DependencyImplementation : IDependency
 {
    public async void RetriveImageFromLocation(string location,Image image)
     {           
        var memoryStream = new MemoryStream();
 
        using (var source = System.IO.File.OpenRead(location))
         {
             await source.CopyToAsync(memoryStream);
          }
 
        image.Source = ImageSource.FromStream(() => memoryStream);
       }
  }

 

 

 

 

 

iOS

 

public void RetriveImageFromLocation(string location,Image image)
  {
 
            string[] str = location.Split('/');
 
            PHFetchResult assetResult = PHAsset.FetchAssetsUsingLocalIdentifiers(str, null);
 
            PHAsset asset = assetResult.firstObject as PHAsset;
 
            PHImageManager.DefaultManager.RequestImageData(asset, null, (data, dataUti, 
            orientation, info) =>
            {
 
                var stream = data.AsStream();
 
                image.Source = ImageSource.FromStream(() => stream);
 
            });
   }

 

 

UWP

 

public async void RetriveImageFromLocation(string location,Image image)
  {
         
            var file = await StorageFile.GetFileFromPathAsync(@""+location);
 
            var stream = await file.OpenStreamForReadAsync();
 
            image.Source = ImageSource.FromStream(() => stream);
     }

 

Step 3: Get the saved image from DependencyService method call and retrieve the image.

 

private void ImageEditor_ImageSaved(object sender, ImageSavedEventArgs args)
{           
 DependencyService.Get<IDependency().RetriveImageFromLocation(args.Location,newImage);        
 }

 

Sample Link:

 

https://www.syncfusion.com/downloads/support/directtrac/general/ze/ImageEditor_GettingStarted736039855.zip

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Alban Favre
Mar 17, 2023

Why not use ? : ImageSource.FromFile(location)

Reply
Eswaran Thirugnanasambandam [Syncfusion]
Mar 20, 2023

Hi Alban Favre,

We can’t use ImageSource.FromFile(location) method to retrieve the image from the saved location on all platforms. However, some platforms restrict access to certain file locations, such as the app's installation directory or other protected locations.

Using dependency service to retrieve the image from the saved location in each platform is a valid approach. By using platform-specific code, you can access the file system in a way that is allowed by the platform and retrieve the image data.

Regards, Eswaran

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile