我正在使用xamarin mono.android版本4,api级别为19.我正在尝试使用以下代码在webview中显示网站我的网页有一个上传文件控件,但使用此应用程序打开时上传文件无法在该网页页面上运行在webview中。当我点击webview中的文件上传控件时,不会显示弹出窗口。
主要活动文件代码
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Webkit;
using webviewUpload;
namespace MyWb
{
[Activity(Label = "MyWb", MainLauncher = true, Icon = "@drawable/icon")]
public class MyWb : Activity
{
public IValueCallback mUploadMessage;
public WebView webview;
public ProgressBar oSpinner;
public static int FILECHOOSER_RESULTCODE = 1;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
WebView webview = FindViewById<WebView>(Resource.Id.webView1);
webview.Settings.JavaScriptEnabled = true;
webview.Settings.AllowFileAccess = true;
webview.LoadUrl("http://www.script-tutorials.com/demos/199/index.html");
webview.SetWebViewClient(new WebViewClient());
webview.SetWebChromeClient(new CustomWebChromeClient(this));
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
if (requestCode == FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage) return;
Android.Net.Uri[] result = data == null || resultCode != Result.Ok ? null : new Android.Net.Uri[] { data.Data };
try
{
mUploadMessage.OnReceiveValue(result);
}
catch (Exception e)
{
}
mUploadMessage = null;
}
base.OnActivityResult(requestCode, resultCode, data);
}
}
public class CustomWebChromeClient : WebChromeClient
{
MyWb WebViewActivity;
public CustomWebChromeClient(MyWb activity)
{
WebViewActivity = activity;
}
public override bool OnShowFileChooser(WebView webView, IValueCallback filePathCallback, FileChooserParams fileChooserParams)
{
WebViewActivity.mUploadMessage = filePathCallback;
Intent i = new Intent(Intent.ActionGetContent);
i.AddCategory(Intent.CategoryOpenable);
i.SetType("*/*");
WebViewActivity.StartActivityForResult(Intent.CreateChooser(i, "File Chooser"), MyWb.FILECHOOSER_RESULTCODE);
return true;
}
}
}
主要xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView1"
android:layout_marginBottom="52.4dp" />
</LinearLayout>
答案 0 :(得分:0)
此代码在Github项目链接中正常工作: https://github.com/WaqarSarfaraz/Xamarin-WebView-FileUpload
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 webview = this.FindViewById<WebView> (Resource.Id.webView1);
webview.SetWebViewClient (new WebViewClient ());
webview.SetWebChromeClient (chrome);
webview.Settings.JavaScriptEnabled = true;
webview.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);
}
}