在Xamarin Android App中使用WebView上传文件 [英] Uploading files using a webview in Xamarin Android App

查看:540
本文介绍了在Xamarin Android App中使用WebView上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的xamarin android应用程序,该应用程序可以显示响应式网站,并接收推送通知.

I'm working on a simple xamarin android app, that allows to display a responsive web site, and receive push notifications.

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上传按钮按预期工作.

Webview seems to have some limitations on Xamarin, so I used the following workaround (https://github.com/GabLeRoux/xamarin-android-webview-upload) based on the following comments (https://forums.xamarin.com/discussion/3259/cannot-override-webviewclient-class-for-file-upload-support) in order to get html upload button work as expected.

一切顺利,直到我将推送通知从GCM迁移到FCM.之后,该Web视图将以开始的状态开始工作.

Everything goes well, until I migrate push notifications from GCM to FCM. After that the webview, returns to work as the beginning.

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

Basically, the html upload button does not open a file chooser dialog, and don't raise any error. It simply does nothing.

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

Following is the code I'm using on the web activity.

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

I'm using Xamarin.Android 7.3, and 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

文件上传将正常工作,我们需要在android清单中授予读/写权限. 在Main Activity.cs中

Step 1

File upload will work , we need to give Read / Write permission in android manifest. in Main Activity.cs

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

Step 3

Add ExtendedChromeClient,cs Inheriting from : 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;
        }
    }
}

这篇关于在Xamarin Android App中使用WebView上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆