Explain in detail the compatibility of Android WebView’s input upload photos

problem

A demand received a few days ago was about H5 uploading photos of third-party financial products.

The other party said that their new needs require the access party to cooperate with the needs of uploading asset photos. After the test, it was found that there was no problem uploading on the app side and IOS side, while there was no response on the Android side.

The other party's H5 call is called through < input type ='File 'accept ='image / *' / >. Originally, I thought this problem was very simple, that is, the app side did not set camera permissions, resulting in no response to clicks. In fact, after adding it, it was found that it was not a simple permission problem.

solve the problem

Because of the fragmentation of the Android version, many versions of WebView have different support for evocation functions.

We need to rewrite openfilechooser () under webchromeclient (system callback onshowfilechooser () of 5.0 and above). We use intent to evoke the system camera and related apps supporting intent in openfilechooser().

After an operation in the system camera or related app, when the app is returned, we return the selected picture to WebView through the onreceivevalue method of valuecallback in onactivityresult().

Attach Code:

1. The first is to rewrite the support of various versions of webchromeclient

2. After selecting photos

3. Tool class

4. Problems needing attention

When typing the release package, there will be no response when clicking because of confusion. This is because openfilechooser () is a system API, so it is necessary not to confuse this method.

After clicking to take a picture, if the camera takes a horizontal picture, when it jumps back to the app after taking a picture, the current WebView page on the app side will be destroyed and reopened. You need to add the following to the current activity in androidmanifest.xml:

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
分享
二维码
< <上一篇
下一篇>>