Skip to content
Advertisement

Android Java Upload File via WebView without FileChooser

I need to write an Android application which is capable of loading a Website via a WebView. The Website contains an Input (Type=FILE)

<form action="...">
    <input type="file" name="myFile" value="" id="..."><
    <input type="submit" value="submit"><
</form>

When loading is done, the application should use a specific Image and upload it via a storage path:

String Path = "/storage/emulated/0/myimage.jpg"

I already tried to open a FileChooser-Dialog and that works, but I need a solution without the filechooser. The path of the “Path”-Variable should be used. I know that this would be a huge security leak, but is there maybe a solution for this? Maybe change die input-value via JavaScript? Maybe with a Library?

PS: Not meant to do anything illegal – the company app is generating profile images and they need to be uploaded via an existing unchangeable Input of type File.

Thanks in advance.

Advertisement

Answer

You can try this:

1) Get the URI of your file.


2) Trigger file chooser


3) Override onShowFileChooser inside WebChromeClient like this:

ValueCallback<Uri[]> mFilePathCallback;
@Override
 public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {

              if (mFilePathCallback != null) {
                    mFilePathCallback.onReceiveValue(null);
                }
                mFilePathCallback = filePathCallback;
                mFilePathCallback.onReceiveValue(myURI);
                mFilePathCallback = null;

                return true;

}

Here myURI is the URI of your file.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement