Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

FilePicker for Android WebView works only once.

SMXamarinSMXamarin Member ✭✭
edited October 21 in Xamarin.Android

I need to upload an image from webview and upload it onto server. The code works, but if I press back without uploading anything, next time the control does not goes in onShowFileChooser.

The same works well on android browser. What is it that I am missing ?

This is what I have implemented :

public class MyWebClient : WebChromeClient
        {
            Activity mContext;

            private IValueCallback mUploadMessage;
            private static int FILECHOOSER_RESULTCODE = 1;

            public MyWebClient(Activity context)
            {
                this.mContext = context;
            }
            [TargetApi(Value = 21)]

private void OnActivityResult(int requestCode, Result resultCode, Intent data)
            {
                if (data != null)
                {
                    if (requestCode == FILECHOOSER_RESULTCODE)
                    {
                        if (null == mUploadMessage || data == null)
                            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 MainActivity;
                mUploadMessage = filePathCallback;
                Intent chooserIntent = fileChooserParams.CreateIntent();
                appActivity.StartActivity(chooserIntent, FILECHOOSER_RESULTCODE, OnActivityResult); 
                return true;
            }

      }

In MainActivity.cs =>

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

Where am I going wrong ?

Best Answer

Answers

  • SMXamarinSMXamarin Member ✭✭

    @Jarvan Thanks. After making relevant changes, I am able to upload images as well as other types of files.

    One thing which I am not able to do is upload multiple files in one go.

    I have put the following line in OnShowFileChooser, but no avail.

    intent .PutExtra(Intent.ExtraAllowMultiple, true);

    It shows no files selected when this line is part of the code. But works only when one file is selected.

Sign In or Register to comment.