Skip to content

Instantly share code, notes, and snippets.

@jonpryor
Last active July 23, 2019 12:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonpryor/5515773 to your computer and use it in GitHub Desktop.
Save jonpryor/5515773 to your computer and use it in GitHub Desktop.
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Webkit;
namespace MyWb
{
[Activity(Label = "MyWb", MainLauncher = true, Icon = "@drawable/icon")]
public class MyWb : Activity
{
int count = 1;
IValueCallback mUploadMessage;
private static int FILECHOOSER_RESULTCODE = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
var chrome = new FileChooserWebChromeClient((uploadMsg, acceptType, capture) => {
mUploadMessage = uploadMsg;
var i = new Intent(Intent.ActionGetContent);
i.AddCategory(Intent.CategoryOpenable);
i.SetType("image/*");
StartActivityForResult(Intent.CreateChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
});
var wv2 = this.FindViewById<WebView>(Resource.Id.webView1);
wv2.SetWebViewClient(new WebViewClient());
wv2.SetWebChromeClient(chrome);
wv2.Settings.JavaScriptEnabled = true;
wv2.LoadUrl("http://www.script-tutorials.com/demos/199/index.html");
}
protected override void OnActivityResult (int requestCode, Result resultCode, Intent intent)
{
if (requestCode==FILECHOOSER_RESULTCODE) {
if (null == mUploadMessage)
return;
Java.Lang.Object result = intent == null || resultCode != Result.Ok
? null
: intent.Data;
mUploadMessage.OnReceiveValue(result);
mUploadMessage = null;
}
}
}
partial class FileChooserWebChromeClient : WebChromeClient
{
Action<IValueCallback, Java.Lang.String, Java.Lang.String> callback;
public FileChooserWebChromeClient (Action<IValueCallback, Java.Lang.String, Java.Lang.String> callback)
{
this.callback = callback;
}
//For Android 4.1
[Java.Interop.Export]
public void openFileChooser(IValueCallback uploadMsg, Java.Lang.String acceptType, Java.Lang.String capture)
{
callback (uploadMsg, acceptType, capture);
}
}
}
@omatrot
Copy link

omatrot commented Jul 23, 2019

Is there any chance that this is still working 4 years later?
At least for me it does not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment