• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Flutter webview_flutter 插件没办法选择文件、图片问题解决

武飞扬头像
BgGod
帮助1

问题描述:

目前flutter中主流的webview插件就是官方的
webview_flutter,但是目前还不支持选择文件和图片,比如当H5页面有选择图片的按钮时,点击是没有反应的。


原因分析:

目前官方还没有写webview相关选择文件或图片的逻辑,这里我们只针对Android端进行处理,需要我们自己拉取其中的webview_flutter_android(这里我用的是2.8.11版本),在WebChromeClient中添加onShowFileChooser方法。


解决方案:

我是基于webview_flutter3.0.4版本修改的,拉取webview_flutter源码将pubspec.yaml文件中的webview_flutter_android引用修改为本地修改后的插件代码路径,如下图:
//For Android 5.0 
    public boolean onShowFileChooser(
            WebView webView, ValueCallback<Uri[]> filePathCallback,
            FileChooserParams fileChooserParams) {
      if (mUploadMessageArray != null) {
        mUploadMessageArray.onReceiveValue(null);
      }
      mUploadMessageArray = filePathCallback;

      final String[] acceptTypes = getSafeAcceptedTypes(fileChooserParams);
      List<Intent> intentList = new ArrayList<Intent>();
      fileUri = null;
      videoUri = null;
      if (acceptsImages(acceptTypes)) {
        Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        fileUri = getOutputFilename(MediaStore.ACTION_IMAGE_CAPTURE);
        takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        intentList.add(takePhotoIntent);
      }
      if (acceptsVideo(acceptTypes)) {
        Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        videoUri = getOutputFilename(MediaStore.ACTION_VIDEO_CAPTURE);
        takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
        intentList.add(takeVideoIntent);
      }
      Intent contentSelectionIntent;
      if (Build.VERSION.SDK_INT >= 21) {
        final boolean allowMultiple = fileChooserParams.getMode() == FileChooserParams.MODE_OPEN_MULTIPLE;
        contentSelectionIntent = fileChooserParams.createIntent();
        contentSelectionIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, allowMultiple);
      } else {
        contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
        contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
        contentSelectionIntent.setType("*/*");
      }
      Intent[] intentArray = intentList.toArray(new Intent[intentList.size()]);

      Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
      chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
      WebViewFlutterPlugin.activity.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
      return true;
    }

    public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
      callback.invoke(origin, true, false);
    }
学新通

学新通

webview_flutter_android修改后的代码已经上传github了,下面附上代码链接以及最终效果,自己尝试的时候修改为自己的相关H5链接:

注意事项:使用时请在自己项目的AndroidManifest.xml文件中添FileProvider文件提供者,具体在demo中的android文件夹里有示例,有问题可以对照一下,这里不做过多解释,有帮到你随手点star即可。

https://github.com/xuechangde/webview_flutter_android_bggod

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfafkh
系列文章
更多 icon
同类精品
更多 icon
继续加载