On the solution of uploading files from Android WebView

When developing requirements, we will inevitably access the H5 pages of third parties. Some H5 pages have the function of uploading photos. WebView in Android cannot directly open the file selection pop-up box

Next, let me briefly provide solutions, and let's talk about ideas first

1. Receive the notification that WebView opens the file selector

2. After receiving the notification, open the file selector and wait for the user to select the file to upload

3. Get the URI of the file selected by the user in onactivityresult

4. Then pass the URI to HTML5

This completes the process of selecting H5 files. Next, I'll post the code for self-study

First, WebView must support JS interaction, so open JS interaction

When H5 calls the API for uploading files, WebView will call back the openfilechooser and onshowfilechooser methods to notify us. What we need to do at this time is to rewrite this method

It should be noted that this method will call back different row parameter methods on different APIs

In the openfilechooser method, we first save the callback object of valuecallback, which is finally used to notify the H5 file address. Then we call the openfilechooser method to open the file selector

After the user selects the file, the onactivityresult method will be called, and we rewrite and wait for the callback

Onactivityresult is used to inform H5 of the file address selected by the user. In this method, use the valuecallback object we saved earlier to call onreceivevalue method, and H5 can receive the address information we passed to it!

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>