Skip to content

Instantly share code, notes, and snippets.

@jfversluis
Created April 16, 2017 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfversluis/f87739e5d5a5b8c826fcfa2c61427823 to your computer and use it in GitHub Desktop.
Save jfversluis/f87739e5d5a5b8c826fcfa2c61427823 to your computer and use it in GitHub Desktop.
Using the camera in a HTML File upload control
public class CustomWebViewRenderer : WebViewRenderer
{
private static int FILECHOOSER_RESULTCODE = 1;
protected override void OnElementChanged (ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged (e);
var chromeClient = new FileChooserWebChromeClient ((uploadMsg, acceptType, capture) => {
MainActivity.UploadMessage = uploadMsg;
// Create MyAppFolder at SD card for saving our images
File imageStorageDir = new File (Android.OS.Environment.GetExternalStoragePublicDirectory (
Android.OS.Environment.DirectoryPictures), "MyAppFolder");
if (!imageStorageDir.Exists ()) {
imageStorageDir.Mkdirs ();
}
// Create camera captured image file path and name, add ticks to make it unique
var file = new File (imageStorageDir + File.Separator + "IMG_"
+ DateTime.Now.Ticks + ".jpg");
MainActivity.mCapturedImageURI = Android.Net.Uri.FromFile (file);
// Create camera capture image intent and add it to the chooser
var captureIntent = new Intent (MediaStore.ActionImageCapture);
captureIntent.PutExtra (MediaStore.ExtraOutput, MainActivity.mCapturedImageURI);
var i = new Intent (Intent.ActionGetContent);
i.AddCategory (Intent.CategoryOpenable);
i.SetType ("image/*");
var chooserIntent = Intent.CreateChooser (i, "Choose image");
chooserIntent.PutExtra (Intent.ExtraInitialIntents, new Intent [] { captureIntent });
((Activity)Forms.Context).StartActivityForResult (chooserIntent, FILECHOOSER_RESULTCODE);
});
Control.SetWebChromeClient (chrome);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment