Plug in to Mono for Android for Lights, Camera and Video Apps: Listing 5

Callback for OnPictureTaken.

public void OnPictureTaken(byte[] data, Android.Hardware.Camera camera)
{
  // Save the image JPEG data to the SD card
  FileOutputStream outStream = null;
  // String dataDir = Android.OS.Environment.DataDirectory.Path;
    File dataDir = Android.OS.Environment.ExternalStorageDirectory;
                
  if (data!=null)
  {
    try
    {
      outStream = new FileOutputStream(dataDir + "/" + PICTURE_FILENAME);
      // outStream = new FileOutputStream("/sdcard/test.jpg");
      outStream.Write(data);
      outStream.Close();
    }
    catch (FileNotFoundException e)
    {
      Android.Util.Log.Debug("SIMPLECAMERA", e.Message);
    }
    catch (IOException e)
    {
      Android.Util.Log.Debug("SIMPLECAMERA", e.Message);
    }
    
    File file = new File(dataDir + "/" + PICTURE_FILENAME);
    try {
                    
      ExifInterface exif = new ExifInterface(file.CanonicalPath);
      // Read the camera model and location attributes
      exif.GetAttribute(ExifInterface.TagModel);
      float[] latLng = new float[2];
      exif.GetLatLong(latLng);
      // Set the camera make
      exif.SetAttribute(ExifInterface.TagMake, "My Phone");
      exif.SetAttribute(ExifInterface.TagDatetime, 
        System.DateTime.Now.ToString());
    } 
    catch (IOException e) {
    
      Android.Util.Log.Debug("SIMPLECAMERA", e.Message);
    }
  }
  else
  {
    Toast.MakeText(this, "No Image Captured", ToastLength.Long);
  }
}

About the Author

Wallace (Wally) B. McClure has authored books on iPhone programming with Mono/Monotouch, Android programming with Mono for Android, application architecture, ADO.NET, SQL Server and AJAX. He's a Microsoft MVP, an ASPInsider and a partner at Scalable Development Inc. He maintains a blog, and can be followed on Twitter.

comments powered by Disqus

Featured

  • .NET for Apache Spark Debuts in Version 1.0

    The open source project .NET for Apache Spark has debuted in version 1.0, finally vaulting the C# and F# programming languages into Big Data first-class citizenship.

  • In-App Reviews Come to Xamarin.Forms Android

    Android is playing a little catch-up to iOS regarding in-app review functionality, just now coming tp Microsoft's Xamarin.Forms implementation.

  • C# Slides in Usage Ranking of Programming Languages

    "The fact that C# lost three places in the ranking of language communities during the last three years is mostly explained by its slower growth compared to C/C++ and PHP."

  • Telerik UI for Blazor Updated

    Progress announced an update to its Telerik UI for Blazor components, targeting Microsoft's open source Blazor framework that lets C# coders create web apps without having to rely upon JavaScript.

  • Infragistics Unveils UI Components for Blazor

    Infragistics, specializing in third-party UI/UX controls and tools, unveiled a new offering targeting Blazor, Microsoft's red-hot open source framework that allows for C#-based web development instead of traditional mainstay JavaScript.

Upcoming Events