javascriptandroidnativescriptnativescript-vue

Nativescript application dont open file chooser on WebView


I have an application Nativescript with a WebView. These WebView open a url, and this url have a input type file. When i touch in this input, nothing happens. I've trying extend the WebChromeClient class and the method onShowFileChooser is called, but nothing happens.

let myWebChromeClientClass = android.webkit.WebChromeClient.extend({
          onShowFileChooser: function (WebView, ValueCallback, FileChooserParams) {
            console.log("onShowFileChooser");
           // What i have to do here?
          }
        });
        let myWebChromeClient = new myWebChromeClientClass();
 webview.android.setWebChromeClient(myWebChromeClient);

I dont know what i have to do.


Solution

  • The problem is that WebView is very limited. But, you can use a plugin to call a file chooser. In my solution i used the imagepicker plugin The complete code is:

    fileCallback(filePathCallback) {
            console.log("fileCallback");
            let context = imagePicker.create({
              mode: "single",
              mediaType: imagePicker.ImagePickerMediaType.Any
            });
            return this.startSelection(context, filePathCallback);
          },
          startSelection(context, filePathCallback) {
            console.log("startSelection");
            let abc = context.authorize().then(() => {
              return context.present();
            })
              .then((selection) => {
                selection.forEach((selected) => {
                  let path = selected.android;
                  let file = fs.File.fromPath(path);
                  this.file_path = file.path;
                  this.file_path = "file://" + this.file_path;
                  let results = Array.create(android.net.Uri, 1);
                  results[0] = android.net.Uri.parse(this.file_path);
                  filePathCallback.onReceiveValue(results);
                });
              }).catch(function (e) {
                console.log(e);
              });
          }
    
         let TNSWebChromeClient = android.webkit.WebChromeClient.extend({
              onShowFileChooser: function (view, valueCallback, fileChooserParams) {
                console.log("onShowFileChooser");
                _this.fileCallback(valueCallback);
                return true;
              }
            });
        let thsWebChromeClient = new TNSWebChromeClient();
         webview.android.setWebChromeClient(thsWebChromeClient);