Created
April 16, 2017 11:16
-
-
Save jfversluis/f87739e5d5a5b8c826fcfa2c61427823 to your computer and use it in GitHub Desktop.
Using the camera in a HTML File upload control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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