在Android nativescript vue webview上使文件输入归档工作

时间:2019-09-30 21:49:35

标签: android nativescript nativescript-vue

Playground上VUE的完整解决方案。


我正在使用NativeScript Vue创建一个应用程序,并且正在使用webview作为主要组件。在已加载的网站中,有一个文件输入字段可捕获摄像机输入。

它在iOS设备上运行良好,但在Android上,输入字段不起作用。

有人知道一种使文件输入字段在Android Webview上运行的解决方案吗?

我使用的是tns-android版本:6.1.1

更新1

我正在使用哪个平台?

  • Android
  • 10.0
  • 模拟器。像素3a

有关堆栈的更多信息:

  • CLI:6.1.2
  • nodejs:v10.16.0
  • tns-core-modules:tns-core-modules@6.1.1
  • 运行时:

      

    “ tns-android”:{         “ version”:“ 6.1.1”       },       “ tns-ios”:{         “ version”:“ 6.1.0”       }

  • 插件:

  

“依赖项”:{

"@nota/nativescript-webview-ext": "^5.4.1",

"@nstudio/nativescript-camera-plus": "^2.2.6",

"axios": "^0.19.0",

"js-cookie": "^2.2.1",

"nativescript-camera": "^4.5.0",

"nativescript-geolocation": "^5.1.0",

"nativescript-plugin-firebase": "^9.1.1",

"nativescript-theme-core": "^1.0.6",

"nativescript-ui-sidedrawer": "^7.0.2",

"nativescript-vue": "~2.4.0",

"net": "^1.0.2",

"rxjs": "^6.5.3",

"tns-core-modules": "~6.1.0",

"vuex": "^3.1.1"
     

},

     

“ devDependencies”:{

"@babel/core": "~7.1.0",

"@babel/preset-env": "~7.1.0",

"babel-loader": "~8.0.0",

"nativescript-dev-webpack": "~1.2.0",

"nativescript-vue-template-compiler": "~2.4.0",

"node-sass": "^4.7.1",

"vue-loader": "~15.4.0"
     

},

在我的Vue文件(加载Webview的文件)中,我这样加载网站:

<webview    @loaded="onWebViewLoaded" :src="webViewSrc"
                     :builtInZoomControls="false"
                     :displayZoomControls="false"
                     :debugMode="true"
/>

然后在 onWebViewLoaded 中尝试执行以下操作: 让myWebChromeClientClass = androidVm.webkit.WebChromeClient.extend({

                    onShowFileChooser: function (WebView, ValueCallback, FileChooserParams) {
                        // FileChooserParams.createIntent()

                        camera.takePicture() // Using nativescript-camera
                            .then(function (imageAsset) {
                                console.log("Result is an image asset instance");
                                var image = new Image();
                                image.src = imageAsset;
                                console.log(image)
                            }).catch(function (err) {
                            console.log("Error -> " + err.message);
                        });

                        return false
                    }
                });
                let myWebChromeClient = new myWebChromeClientClass();
                webView.android.setWebChromeClient(myWebChromeClient);

当前,我正在努力使拍照后的nativescript-camera行为(总是返回“取消”),但是我知道有一种方法可以使用FileChooserParams显示本地文件输入选择,但我无法做到这一点可以作为我在网上找到的最接近Java的解决方案,也不能确定如何将其转换为Javascript / Vue。

还有其他想法吗?

1 个答案:

答案 0 :(得分:1)

您将必须扩展android.webkit.WebChromeClient并在nativeView上进行分配。

您将必须实现onShowFileChooser方法,使用Intent或其他方法来处理文件的选择。