![](/img/trans.png)
[英]Uploading file not working using a webview in Xamarin Android App
[英]Uploading files using a webview in Xamarin Android App
我正在開發一個簡單的 xamarin android 應用程序,它允許顯示響應式網站並接收推送通知。
Webview 似乎對 Xamarin 有一些限制,所以我根據以下評論( https://forums.xamarin.com/ )使用了以下解決方法( https://github.com/GabLeRoux/xamarin-android-webview-upload ) 討論/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
}
}
文件上傳將起作用,我們需要在 android manifest 中授予讀/寫權限。 在主 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;
}
添加 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;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.