首页 > 解决方案 > WebView 文件选择器在取消选择后停止响应

问题描述

我们已经为 Web 视图实现了文件选择器。选择附件时它可以成功运行,但在没有文件说明的情况下取消它会失败。文件选择器只是停止对点击做出反应

任何帮助表示赞赏。谢谢

我们使用 chrome 客户端。如果在所有情况下都列出了文件选择,则它工作正常。但即使从第一个文件选择被取消,文件选择器也不再起作用。它是完全基于 webview 的 Xamarin.Android 应用程序

我们的代码是:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
{
    if (requestCode == FILECHOOSER_RESULTCODE)
    {
        if (null == _mUploadMessage)
            return;

        // Check that the response is a good one
        if (resultCode == Result.Ok)
        {
            Android.Net.Uri[] results = null;
            if (intent == null)
            {
                // If there is not data, then we may have taken a photo
                if (mCameraPhotoPath != null)
                {
                    results = new Android.Net.Uri[] { Android.Net.Uri.Parse(mCameraPhotoPath) };
                }
            }
            else
            {
                if (intent.DataString != null)
                {
                    results = new Android.Net.Uri[] { Android.Net.Uri.Parse(intent.DataString) };
                }
            }

            _mUploadMessage.OnReceiveValue(results);
            _mUploadMessage = null;
        }
    }
}

铬客户端:

        var chrome = new FileChooserWebChromeClient((uploadMsg) =>
        {
            _mUploadMessage = uploadMsg;

            mCameraPhotoPath = null;

            Intent takePictureIntent = new Intent(Android.Provider.MediaStore.ActionImageCapture);

            //Create the File where the photo should go
            File photoFile = null;
            try
            {
                string folder = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
                photoFile = new File(folder, "image" + DateTime.Now.Millisecond + ".png");
                takePictureIntent.PutExtra("PhotoPath", mCameraPhotoPath);
            }
            catch (IOException ex)
            {
                // Error occurred while creating the File
                System.Console.WriteLine("" + ex.ToString());
            }

            // Continue only if the File was successfully created
            if (photoFile != null)
            {
                mCameraPhotoPath = "file:" + photoFile.AbsolutePath;
                takePictureIntent.PutExtra(Android.Provider.MediaStore.ExtraOutput,
                        Android.Net.Uri.FromFile(photoFile));
            }
            else
            {
                takePictureIntent = null;
            }

            Intent contentSelectionIntent = new Intent(Intent.ActionGetContent);
            contentSelectionIntent.AddCategory(Intent.CategoryOpenable);
            contentSelectionIntent.SetType("image/*");

            Intent[] intentArray;
            if (takePictureIntent != null)
            {
                intentArray = new Intent[] { takePictureIntent };
            }
            else
            {
                intentArray = new Intent[0];
            }

            Intent chooserIntent = new Intent(Intent.ActionChooser);
            chooserIntent.PutExtra(Intent.ExtraIntent, contentSelectionIntent);
            chooserIntent.PutExtra(Intent.ExtraTitle, this.GetStringFromResource(Resource.String.chose_photo));
            chooserIntent.PutExtra(Intent.ExtraInitialIntents, intentArray);

            base.StartActivityForResult(chooserIntent, HarmonyAndroid.AndroidMainActivity.FILECHOOSER_RESULTCODE);
        });

        return chrome;

第2部分

class FileChooserWebChromeClient : WebChromeClient
{
    Action<IValueCallback> callback;

    public FileChooserWebChromeClient(Action<IValueCallback> callback)
    {
        this.callback = callback;
    }

    public override bool OnShowFileChooser(WebView webView, IValueCallback filePathCallback, FileChooserParams fileChooserParams)
    {
        callback(filePathCallback);
        return true;
    }

    public override void OnCloseWindow(WebView window)
    {
        base.OnCloseWindow(window);
    }
}

第 3 部分

   webView.ImprovePerformance();
   webView.SetWebViewClient(new HomeWebViewClient(customWebViewClientListener, clientId));
   webView.SetWebChromeClient(chrome);
   webView.Settings.JavaScriptEnabled = true;
   webView.Settings.DomStorageEnabled = true;
   webView.SetDownloadListener(new CustomDownloadListener(activity, customDownloadListener));
   webView.AddJavascriptInterface(new JavaScriptToCSharpCommunication(activity, javaScriptToCSharpCommunicationListener), Constants.JS_CSHARP_COMMUNICATOR_NAME);

标签: xamarinxamarin.android

解决方案


resultCode当is not时,尝试给 uri 回调一个 null 对象RESULT_OK

添加您的OnActivityResult方法:

if (resultCode != Result.Ok)
 {
    _mUploadMessage.OnReceiveValue(null);
    _mUploadMessage = null;
    return;
 }

推荐阅读