locked
FilePicker for Android WebView works only once. RRS feed

  • Question

  • User395800 posted

    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 ?

    Tuesday, October 20, 2020 1:58 PM

Answers

  • User382871 posted

    To upload an image in webview, try the following code: MainActivity class public class MainActivity : AppCompatActivity { public static IValueCallback mUploadMessage; public static int FILECHOOSER_RESULTCODE = 1; ... protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent) { if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; Android.Net.Uri result = intent == null || resultCode != Result.Ok ? null : intent.Data; mUploadMessage.OnReceiveValue(WebChromeClient.FileChooserParams.ParseResult((int)resultCode, intent)); mUploadMessage = null; } } } Custom WebChromeClient class. ``` public class CustomWebChromeClient : WebChromeClient { Activity mActivity = null; public CustomWebChromeClient(Activity activity) { mActivity = activity; } public override bool OnShowFileChooser(Android.Webkit.WebView webView, IValueCallback filePathCallback, FileChooserParams fileChooserParams) { MainActivity.mUploadMessage = filePathCallback;

        var intent = new Intent(Intent.ActionGetContent);
        intent.AddCategory(Intent.CategoryOpenable);
        intent.SetType("image/*");
        mActivity.StartActivityForResult(intent, MainActivity.FILECHOOSER_RESULTCODE);
    
        return true;
    }
    

    } ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, October 21, 2020 8:46 AM

All replies

  • User382871 posted

    To upload an image in webview, try the following code: MainActivity class public class MainActivity : AppCompatActivity { public static IValueCallback mUploadMessage; public static int FILECHOOSER_RESULTCODE = 1; ... protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent) { if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; Android.Net.Uri result = intent == null || resultCode != Result.Ok ? null : intent.Data; mUploadMessage.OnReceiveValue(WebChromeClient.FileChooserParams.ParseResult((int)resultCode, intent)); mUploadMessage = null; } } } Custom WebChromeClient class. ``` public class CustomWebChromeClient : WebChromeClient { Activity mActivity = null; public CustomWebChromeClient(Activity activity) { mActivity = activity; } public override bool OnShowFileChooser(Android.Webkit.WebView webView, IValueCallback filePathCallback, FileChooserParams fileChooserParams) { MainActivity.mUploadMessage = filePathCallback;

        var intent = new Intent(Intent.ActionGetContent);
        intent.AddCategory(Intent.CategoryOpenable);
        intent.SetType("image/*");
        mActivity.StartActivityForResult(intent, MainActivity.FILECHOOSER_RESULTCODE);
    
        return true;
    }
    

    } ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, October 21, 2020 8:46 AM
  • User395800 posted

    @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.

    Thursday, October 22, 2020 7:53 AM