File Upload from Webview on Android

You have a form on WebView, with input type as “file”, and want to allow the user to upload a file from his device. Prior to Lollipop, this was not very straightforward, and you had to resort to using private APIs, which has always been discouraged. But, you had to do it. With Lollipop, there’s a new public API, which still isn’t very straight-forward to use, but at-least, should be reliable and compatible with newer Android/WebView versions. So, how do you implement File Upload from WebView on Android with the new public API? 

This has been a long pending request, and people have implemented workarounds by using various versions of some private API’s for various older versions of Android.

Steps to Implement (3 steps)

  • Attach WebViewChromeClient, and override method callback for a click event on and input field of type file.
mWebView.setWebChromeClient(new WebChromeClient() {

         public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,              FileChooserParams fileChooserParams) {
                 mFilePathCallback = filePathCallback;
                // Launch Intent for picking file
                return true;
  • Launch Intent for picking the file
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
  • Pass the Uri of the selected file to WebView
protected void onActivityResult(int requestCode, int resultCode,
     Intent intent) {
     if (requestCode == PICKFILE_REQUEST_CODE) {
          Uri result = intent == null || resultCode != RESULT_OK ? null
              : intent.getData();
          Uri[] resultsArray = new Uri[1];
          resultsArray[0] = result;


I have created a small sample project with all the required code to achieve a simple input field taking in an image. You can get the source on Github.

TextView with HTML content with Images

Handling HTML content on a TextView is simple as far as the HTML coming in contains a few tags that are by default supported by Android. Simple formatting like bold, italics, font sizes can be handled without even coding a single extra line.

Say, if you have a TextView tv, and there’s some HTML string with bold and italicized text, bringing it up on the TextView is pretty simple.

One line code for that:


where the source is actually your HTML string. This works perfectly. But how do we show images if there are any. Well, it’s a bit tricky. You have to use the other method that takes in an ImageGetter and a TagHandler.

fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)

The tagHandler is for situations where you wish to handle specific tags differently. I didn’t wish to do that, so I just passed null there.

Now comes the main task. How do you get the image on to the TextView!!!!

Implement the Html.ImageGetter’s getDrawable method which handles downloading the image, or accessing it from the net, and then create a drawable and return that object.

      static ImageGetter imgGetter = new Html.ImageGetter() {
             public Drawable getDrawable(String source) {
                   Drawable drawable = null;
                   drawable = Drawable.createFromPath(source);  // Or fetch it from the URL
                   // Important
                   drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
                   return drawable;

and use the method on the TextView like this.
tv.setText(Html.fromHtml(source, imgGetter, null);

This will load the TextView with the image. But this call to the getDrawable method is not asynchronous. So, until and unless that method returns, you UI will be blocked. In my case, I am creating the drawable from a local image, so, it didn’t take much time. But, if you want to fetch an image from the web, you have to make this call in a separate thread, so that the UI is not blocked.

So, check your HTML string if they contain any images that have to be downloaded. If you find any, create a thread that download that image, saves it somewhere and returns you the location of that file. Now, change the src tags to point to the local images, and call setText method on the TextView.

And that should do it. The important thing to remember is, you have to change the HTML to point it to the file that you have downloaded.

Sample Source code : 

This sample doesn’t use threads. So, your UI will be blocked unitl the image here is downloaded. So, keep waiting. 🙂