)
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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to Load an Image from Gallery in ImageEditor Xamarin.Android

Platform: Xamarin.Android |
Control: SfImageEditor |
Published Date: June 21, 2017 |
Last Revised Date: June 25, 2019

How to Load an Image from Gallery in ImageEditor Xamarin.Android?

Description:

This article explains you to load an Image from Gallery to the Image Editor.

 

Solution:

  1. Create a Xamarin application and initialize SfImageEditor control in it by using the below code.

 

Import the SfImageEditor as shown below,

 

 

 

using Syncfusion.SfImageEditor.Android;

 

 

C#

 

static Intent mainIntent;

private static int SELECT_FROM_GALLERY = 0;

internal static string Path { get; set; }

SfImageEditor editor;

 

protected override void OnCreate(Bundle bundle)

{

    base.OnCreate(bundle);

    editor = new SfImageEditor(this);

    SetContentView(editor);

 

}

 

  1. Load an Image from Gallery to the image editor by call the InitializeMediaPicker method as shown in below.

 

 

C#

 

public void InitializeMediaPicker()
{
    Intent = new Intent();
    Intent.SetType("image/*");
    Intent.SetAction(Intent.ActionGetContent);
    StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), SELECT_FROM_GALLERY);
}

 

 

 

 

 

private string GetPathToImage(Android.Net.Uri uri)
{
    string imgId = "";
    using (var c1 = ContentResolver.Query(uri, null, null, null, null))
    {
        c1.MoveToFirst();
        string imageId = c1.GetString(0);
        imgId = imageId.Substring(imageId.LastIndexOf(":") + 1);
    }
 
    string path = null;
 
    string selection = MediaStore.Images.Media.InterfaceConsts.Id + " =? ";
    using (var cursor = ContentResolver.Query(MediaStore.Images.Media.ExternalContentUri, null, selection, new string[] { imgId }, null))
    {
        if (cursor == null) return path;
        var columnIndex = cursor.GetColumnIndexOrThrow(MediaStore.Images.Media.InterfaceConsts.Data);
        cursor.MoveToFirst();
        path = cursor.GetString(columnIndex);
    }
    return path;
}

 

 

  1. Set the Captured image from Camera to the ImageEditor as Bitmap Only. So you need to set the Bitmap as shown in below

 

protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
    if (data == null) data = mainIntent;
    if ((resultCode != Result.Ok) || (data == null))
    {
        return;
    }
    if (resultCode == Result.Ok)
    {
        var uri = data.Data;
 
        if (requestCode == SELECT_FROM_GALLERY)
        {
            try
            {
                Path = GetPathToImage(uri);
                editor.Bitmap = BitmapFactory.DecodeFile(Path);
 
            }
            catch (Exception ex)
            {
 
            }
        }
 
        else return;
    }
}

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

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
Live Chat Icon