Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Html File Input #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion SampleApp/SampleApp.Droid/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace SampleApp.Droid
{
[Activity(Label = "SampleApp", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, IMainActivityWithStarting
{
protected override void OnCreate(Bundle bundle)
{
Expand All @@ -27,6 +27,22 @@ protected override void OnCreate(Bundle bundle)
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}

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

29 changes: 28 additions & 1 deletion Xam.Plugin.WebView.Droid/FormsWebViewChromeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,45 @@
using Android.Views;
using Android.Widget;
using Android.Webkit;
using Xamarin.Forms.Platform.Android;

namespace Xam.Plugin.WebView.Droid
{
public class FormsWebViewChromeClient : WebChromeClient
{

private IValueCallback mUploadMessage;
private static int FILECHOOSER_RESULTCODE = 1;
readonly WeakReference<FormsWebViewRenderer> Reference;

public FormsWebViewChromeClient(FormsWebViewRenderer renderer)
{
Reference = new WeakReference<FormsWebViewRenderer>(renderer);
}

/*
* below lines for fixing issue #66: Not support HTML Input File
* reference this workaround: https://forums.xamarin.com/discussion/62284/input-type-file-doesnt-work-on-xamarin-forms-webview-androidhome.firefoxchina.cn
*/
private void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if (requestCode == FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage)
return;
mUploadMessage.OnReceiveValue(WebChromeClient.FileChooserParams.ParseResult((int)resultCode, data));
mUploadMessage = null;
}
}

[Android.Runtime.Register("onShowFileChooser", "(Landroid/webkit/WebView;Landroid/webkit/ValueCallback;Landroid/webkit/WebChromeClient$FileChooserParams;)Z", "GetOnShowFileChooser_Landroid_webkit_WebView_Landroid_webkit_ValueCallback_Landroid_webkit_WebChromeClient_FileChooserParams_Handler")]
public override bool OnShowFileChooser(Android.Webkit.WebView webView, IValueCallback filePathCallback, FileChooserParams fileChooserParams)
{
var appActivity = Xamarin.Forms.Forms.Context as IMainActivityWithStarting;
mUploadMessage = filePathCallback;
Intent chooserIntent = fileChooserParams.CreateIntent();
appActivity.StartActivity(chooserIntent, FILECHOOSER_RESULTCODE, OnActivityResult);
//return base.OnShowFileChooser (webView, filePathCallback, fileChooserParams);
return true;
}
}
}
19 changes: 19 additions & 0 deletions Xam.Plugin.WebView.Droid/IMainActivityWithStarting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

namespace Xam.Plugin.WebView.Droid
{
public interface IMainActivityWithStarting
{
void StartActivity(Intent intent, int requestCode, Action<int, Result, Intent> resultCallback);
}
}
1 change: 1 addition & 0 deletions Xam.Plugin.WebView.Droid/Xam.Plugin.WebView.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<Compile Include="FormsWebViewChromeClient.cs" />
<Compile Include="FormsWebViewClient.cs" />
<Compile Include="FormsWebViewRenderer.cs" />
<Compile Include="IMainActivityWithStarting.cs" />
<Compile Include="JavascriptValueCallback.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down