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 load an image from the camera to image editor in Xamarin.Android

Platform: Xamarin.Android |
Control: SfImageEditor |
Published Date: June 21, 2017 |
Last Revised Date: April 9, 2020

This article shows how to load an image from camera to the SfImageEditor control as per the following mentioned steps

 

Step 1:

 

Initially, you need to make sure that proper folders exist in the memory of the device where we store our captured images. If that does not exist, you will create a folder as follows

 

[C#]

        private void CreateDirectory()
        {
            directory = new File(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures), "CameraApp");
            if (!directory.Exists())
            {
                directory.Mkdirs();
            }
        }

 

Step 2:

 

Initialize an intent with ActionImageCapture action and store the captured image to the file by calling the PutExtra() method of the intent. Finally to launch the intent by calling the StartActivityForResult() by passing the intent and a request code.

 

[C#]

        private void InitializeCamera(object sender, EventArgs eventArgs)
        {
 
            Intent intent = new Intent(MediaStore.ActionImageCapture);
            var documentsDirectry = ApplicationContext.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures);
            cameraFile = new Java.IO.File(documentsDirectry, "default_image" + "." + FileFormatEnum.JPEG.ToString());
            Android.Net.Uri photoURI = FileProvider.GetUriForFile(ApplicationContext, ApplicationContext.PackageName+".provider", cameraFile);
            intent.PutExtra(MediaStore.ExtraOutput, photoURI);        
            StartActivityForResult(intent, 0);
        }

 

Step 3:

 

After an image is captured OnActivityResult() calls to handle the data received by the camera. Set the captured image from camera to the ImageEditor as bitmap only. So, you need to set the bitmap as follows.

 

[C#]

        protected override void OnActivityResult(int requestCode, Result resultCode,     Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            if (bitmap != null)
            {
                bitmap.Dispose();
            }
            bitmap = GetBitmap(cameraFile.Path);
            editor.Bitmap = bitmap;
        }
 
        public static Bitmap GetBitmap(string fileName)
        {
            BitmapFactory.Options options = new BitmapFactory.Options
            {
                InPurgeable = true,
                InJustDecodeBounds = false
            };
            Bitmap resizedBitmap = BitmapFactory.DecodeFile(fileName, options);
 
            return resizedBitmap;
        }

 

Step 4:

 

Add the permissions and add the following to your AndroidManifest.xml inside the <application> tags.

 

[XML]

   <uses-permission android:name="android.permission.CAMERA" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
    <provider
          android:name="android.support.v4.content.FileProvider"
          android:authorities="${applicationId}.provider"
          android:exported="false"
          android:grantUriPermissions="true">
      <meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/file_paths"/>
    </provider>
  </application>

 

Step 5:

 

Add a new folder called xml into your Resources folder and add a new XML file called file_paths.xml

Add the following code.

 

[XML]

<?xml version="1.0" encoding="UTF-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="." />
</paths>
 

Output

Before loading an image from camera

 

SfImageEditor before loading an image from camera.

 

After the image is loaded from the camera

 

SfImageEditor after the image is loaded from the camera.

 

Download the complete sample here.

 

See Also:

 

How do I add a shape to an image?

 

How do I notify the image that is loaded in the SfImageEditor control?

 

How can I customize the appearance of the shape?

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