File Upload in WebView | Android

<p>Sometimes we want to show some web views in our app and handle them using a javascript bridge. Instead of doing everything natively.</p> <p>To understand more about webView please visit the below link.</p> <h2><a href="https://developer.android.com/develop/ui/views/layout/webapps/webview?source=post_page-----26c10ff27aee--------------------------------" rel="noopener ugc nofollow" target="_blank">Build web apps in WebView | Android Developers</a></h2> <h3><a href="https://developer.android.com/develop/ui/views/layout/webapps/webview?source=post_page-----26c10ff27aee--------------------------------" rel="noopener ugc nofollow" target="_blank">Edit description</a></h3> <p><a href="https://developer.android.com/develop/ui/views/layout/webapps/webview?source=post_page-----26c10ff27aee--------------------------------" rel="noopener ugc nofollow" target="_blank">developer.android.com</a></p> <p>This will give you a Lil bit of understanding of how to use Webview in-app, as well as how to handle URLs and navigation between diff URLs.</p> <p>Now sometimes in your webView, you might see some photo upload option or a dialog. How do we handle those? In this case, when you click on upload photo, WebView fires a JS event that tells the native system to perform a certain action. These callbacks are part of WebChromeClient.</p> <h1>What is WebChromeClient</h1> <p>It is used to handle Java script events which is triggered by WebView.</p> <p>Following are the basic events that WebChromeClient Handles:</p> <ul> <li><strong>onJsAlert</strong>(WebView view, String url, String message, JsResult result)</li> <li><strong>onJsConfirm</strong>(WebView view, String url, String message, JsResult result)</li> <li><strong>onPermissionRequest</strong>(PermissionRequest request)</li> <li><strong>onShowFileChooser</strong>(WebView webView, ValueCallback&lt;Uri[]&gt; filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)</li> </ul> <p>You can see all other event callbacks&nbsp;<a href="https://developer.android.com/reference/android/webkit/WebChromeClient" rel="noopener ugc nofollow" target="_blank">HERE</a>.</p> <p>OnShowFileChooser is what we are gonna talk about today. This is the event that we receive from WebView when any file needs to be uploaded.</p> <p>Let&rsquo;s see an example where we have events page as WebView. Now while posting any post we also want to upload a picture related to that event. We see an upload button, we click on it and we get an onShowFileChooser event callback.</p> <p>Now here we will do a few things</p> <ol> <li>Receive the event</li> <li>Set FileChoserParams, which provides you the facility to choose your action like which kind of file you want to upload, jpg or png or a video file, or any time works. As well as you can choose a mode like from the gallery you can select multiple images or just one. And if it&#39;s a newly created file, do you want to save that picture in the gallery or do you want to remove it once uploaded?</li> <li>Save or delete the file once uploaded.</li> <li>Do the whole process in the coroutine Scope.</li> </ol> <p><a href="https://karishma-agr1996.medium.com/file-upload-in-webview-android-26c10ff27aee">Read More</a>&nbsp;</p>