Articles in this section
Category / Section

How to load an image from the camera to Image Editor in Xamarin.Android?

6 mins read

This article shows how to load an image from camera to the Xamarin.Android ImageEditor 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?


Conclusion

I hope you enjoyed learning about how to load an image from the camera to image editor in Xamarin.Android.

You can refer to our Xamarin.Android Image Editor feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.Android Image Editor documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied