Xamarin Android网络视图文件选择器

时间:2018-10-08 09:10:25

标签: android xamarin webview filechooser

我编写了一个混合应用程序,我想使用从HTML(文件和相机)页面上传文件。我发现了很多代码,并在下面使用了这一代码(效果很好)

当用户单击FileUploader输入时,本机窗口打开(用于在File或Camera之间进行选择),一切正常:

我唯一的问题是当用户单击窗口中的 时,该窗口将关闭,并且在不再打开后,可能需要重新初始化或重新启动...

如果有人有想法...

谢谢

在MainActivity上:

Optional

...

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

和我的WebChromeClient类:

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

1 个答案:

答案 0 :(得分:-1)

我像下面的代码一样解决了您的问题 将其他部分放到结果不正确

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