在Xamarin Android App中使用webview上传文件

时间:2017-07-31 16:59:24

标签: c# visual-studio webview xamarin.android

我正在开发一个简单的xamarin android应用程序,它允许显示响应式网站,并接收推送通知。

Webview似乎对Xamarin有一些限制,因此我根据以下注释(https://github.com/GabLeRoux/xamarin-android-webview-upload)使用了以下解决方法(https://forums.xamarin.com/discussion/3259/cannot-override-webviewclient-class-for-file-upload-support),以使html上传按钮按预期工作。

一切顺利,直到我将推送通知从GCM迁移到FCM。之后,webview将返回工作作为开始。

基本上,html上传按钮不会打开文件选择器对话框,也不会引发任何错误。它什么都不做。

以下是我在网络活动中使用的代码。

我正在使用Xamarin.Android 7.3和VS 2015。

欢迎任何帮助。

using System;
using Android.Runtime;
using Android.Views;
using Android.App;
using Android.Content;
using Android.OS;

using Android.Webkit;
using Android.Widget;



namespace sigese
{
    [Activity(Label = "WebActivity")]
    public class WebActivity : Activity
    {


        IValueCallback mUploadMessage;
        private static int FILECHOOSER_RESULTCODE = 1;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.WebLayout);

            ActionBar.Hide();

            var username = Intent.GetStringExtra("username");
            var password = Intent.GetStringExtra("password");

            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.LocalWebView);
            webview.SetWebViewClient(new WebViewClient());
            webview.SetWebChromeClient(chrome);
            webview.Settings.JavaScriptEnabled = true;


           webview.LoadUrl("https://example.com/login.asp?username="+username+"&password="+password);




        }

        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;
            }
        }

        public override void OnBackPressed()
        {
            WebView localWebView = FindViewById<WebView>(Resource.Id.LocalWebView);
            if (localWebView.CanGoBack())
            {
                localWebView.GoBack();
            }
            else
            {
               return;
            }
        }


    }
    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 < 5.0
        [Java.Interop.Export]
        public void openFileChooser(IValueCallback uploadMsg, Java.Lang.String acceptType, Java.Lang.String capture)
        {
            callback(uploadMsg, acceptType, capture);
        }

        // For Android > 5.0

    }
}

1 个答案:

答案 0 :(得分:2)

第1步

文件上传将起作用,我们需要在android清单中给予读/写权限。 在主Activity.cs

第2步

private Action<int, Result, Intent> resultCallbackvalue;

public void StartActivity(Intent intent, int requestCode, Action<int, Result, Intent> resultCallback)
{
    this.resultCallbackvalue = resultCallback;
    StartActivityForResult(intent, requestCode);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    if (this.resultCallbackvalue != null)
{
   this.resultCallbackvalue(requestCode, resultCode, data);
   this.resultCallbackvalue = null;
}

第3步

添加ExtendedChromeClient,cs继承自:WebChromeClient

private static int filechooser = 1;
private IValueCallback message;
private MainActivity activity = null;

public ExtendedChromeClient(MainActivity context)
{
    this.activity = context;
}

public override bool OnShowFileChooser(WebView webView, IValueCallback filePathCallback, FileChooserParams fileChooserParams)
{
    this.message = filePathCallback;
    Intent chooserIntent = fileChooserParams.CreateIntent();
    chooserIntent.AddCategory(Intent.CategoryOpenable);
    this.activity.StartActivity(Intent.CreateChooser(chooserIntent, "File Chooser"), filechooser, this.OnActivityResult);
    return true;
}

private void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    if (data != null)
    {
        if (requestCode == filechooser)
        {
            if (null == this.message)
            {`enter code here`
                    return;
            }

            this.message.OnReceiveValue(WebChromeClient.FileChooserParams.ParseResult((int)resultCode, data));
            this.message = null;
        }
    }
}