gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:39:02 25 4
gpt4 key购买 nike

我正在开发一个简单的 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

}
}

最佳答案

第一步

文件上传可以,我们需要在 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;
}
}
}

关于c# - 在 Xamarin Android App 中使用 webview 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45421528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com