Android WebView supports full parsing of uploaded files

By default, WebView using Android cannot support uploading files. This was learned after our front-end engineer informed us. Because the implementation of WebView in each version of Android is different, it needs to be adapted to different versions. I spent some time referring to other people's code. This problem has been solved. Here I share the pit I stepped on. The main idea is to rewrite the webchromeclient, then receive the selected file URI in webviewactivity and send it to the page to upload. Create an internal class of webviewactivity

The above openfilechooser is an unexposed interface of the system, so there is no need to add the annotation of override. At the same time, different versions have different parameters. Among them, the first valuecallback is used to receive the file and call it back to the web page for processing after selecting the file. Accepttype is the accepted file MIME type. After Android 5.0, the system provides onshowfilechooser to let us implement the method of selecting files. There is still valuecallback. The filechoosperparams parameter also includes accepttype. We can open the system or create our own file selector according to the accepttype. Of course, if you need to turn on the camera to take pictures, you can also use the intention to turn on the camera to take pictures. To process the selected file, the above is the interface to open the response file selection. We also need to process the received file and send it to the web page for response. Because we used startactivityforresult to open the selection page, we will receive the selection result in onactivityresult. Show code:

The above code mainly calls the onreceivevalue method of valuecallback to send the result back to the web. Note that it is important to note that due to the differences between different versions, the parameter type received by onreceivevalue of valuecallback in Android versions below 5.0 is URI, and that received by versions 5.0 and above is URI array. You should pay attention to it when transmitting values. Selecting a file will use the components provided by the system or other supported apps. Some of the returned URIs are directly the URL of the file, and some are the URI of the ContentProvider. Therefore, we need to handle it uniformly and convert it into the URI of the file. Please refer to the following code (get the path of the file). Calling getpath can convert the URI to the path of the real file, and then you can generate the URI of the file yourself

In addition, even if the obtained result is null, it should also be passed to the web, that is, directly call muplodmessage.onreceivevalue (null), otherwise the web page will be blocked. Finally, when typing the release package, because we will be confused, we should make special settings not to confuse the openfilechooser method in the webchromeclient subclass. Because it is not an inherited method, it will be confused by default, and then we can't select files. be it so. Original address: http://blog.isming.me/2015/12/21/android-webview-upload-file/

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