简体   繁体   English

Xamarin Android Web 视图文件选择器

[英]Xamarin Android Web-view File Chooser

I wrote a hybrid app and I want to use file upload HTML (file and camera) page.我写了一个混合应用程序,我想使用文件上传 HTML(文件和相机)页面。 I use the below code and it works well.我使用下面的代码,它运行良好。

When the user clicks on FileUploader input, the native window opens (to choose between File or Camera) and everything works just fine.当用户单击 FileUploader 输入时,本机窗口将打开(在文件或相机之间进行选择),一切正常。

My only problem is that, when user click out of the window , this one will be closed and after no more opened, maybe something to reinitialise or restart.我唯一的问题是,当用户单击窗口外时,此窗口将关闭,不再打开后,可能需要重新初始化或重新启动。

If someone has some ideas...如果有人有一些想法......

On MainActivity:在 MainActivity 上:

var chrome = new SmarterWebChromeClient((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
                            {
                                photoFile = createImageFile();
                                takePictureIntent.PutExtra("PhotoPath", mCameraPhotoPath);
                            }
                            catch (IOException ex)
                            {
                                // Error occurred while creating the File
                                writeEx(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, "Choisir une photo");
                            chooserIntent.PutExtra(Intent.ExtraInitialIntents, intentArray);

                            StartActivityForResult(chooserIntent, MainActivity.FILECHOOSER_RESULTCODE);

                        });


                        mWebView.SetWebViewClient(new MyWebViewClient());

                        mWebView.SetWebChromeClient(chrome);

... ...

  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
                            {

                            }
                        }
                        else
                        {
                            if (intent.DataString != null)
                            {
                                results = new Android.Net.Uri[] { Android.Net.Uri.Parse(intent.DataString) };
                            }
                        }

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

And my WebChromeClient Class :还有我的 WebChromeClient 类:

 partial class SmarterWebChromeClient : WebChromeClient
        {
            Action<IValueCallback> callback;

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

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

I solve your problem like below code Put an else part for result NOT OK我像下面的代码一样解决了你的问题

// Check that the response is a good one
if (resultCode == Result.Ok)
{
   //...
}
else
{
   mUploadMessage.OnReceiveValue(null);
   mUploadMessage = null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM