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

  • Visual Studio 2019 for Mac v8.9 Ships with .NET 6 Preview 1 Support

    During its Ignite 2021 online event for IT pros and developers this week, Microsoft shipped Visual Studio 2019 for Mac v8.9, arriving with out-of-the-box support for .NET 6 Preview 1, which the company also released recently.

  • Analyst: TypeScript Now Firmly in Top 10 Echelon (Ruby, Not So Much)

    RedMonk analyst Stephen O'Grady believes TypeScript has achieved the rare feat of firmly ensconcing itself into the top 10 echelon of his ranking, now questioning how high it might go.

  • Black White Wave IMage

    Neural Regression Using PyTorch: Training

    The goal of a regression problem is to predict a single numeric value, for example, predicting the annual revenue of a new restaurant based on variables such as menu prices, number of tables, location and so on.

  • Microsoft Ships Visual Studio 2019 v16.9 Servicing Baseline Release

    Microsoft is urging enterprises and professional coders to standardize on the new Visual Studio 2019 v16.9, a servicing baseline release that's guaranteed to receive official support for an extended period.

Upcoming Events