File Upload from Webview on Android

android lollipopYou 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.

Script for compressing images with tinypng API

TinyPng is a nice service which provides you 500 free image compressions a month. And this is nice, since in a typical Android project, you wouldn’t have more than 500 images in any given month :). I have a simple script that scans through all your images, and tries to compress them using the tinypng api. All you need to do is to copy the script, put it in the root folder of your project and run it.

Head over to TinyPng website to checkout how it works.

What does TinyPNG do?

TinyPNG uses smart lossy compression techniques to reduce the file size of your PNG files. By selectively decreasing the number of colors in the image, fewer bytes are required to store the data. The effect is nearly invisible but it makes a very large difference in file size!

Advanced lossy compression for PNG images that preserves full alpha transparency.

Let’s jump into it. This script will search for all the images(pngs) in your Android project, pick each one, call the API, and overwrite the images, with a compressed version. And since this is a lossless compression, you won’t notice any change in quality. The overwriting part is probably a bit dangerous. So, with a little modification, you could change the script so that it creates a copy of the files.

The initial script for tinypng was taken from this gist by rakuishi. Thanks :).

Image Crush Savings Report
  1. The first thing you would need to do is to get an API key here.
  2. Copy this file to the root of your android folder.
  3. Replace your API key in this file on the top.
  4. Run “ruby imgcrush.rb”
  5. View the generated HTML report. (Link to sample report)

That’s it. For an example above, you could see, the script reduced the size to almost half for each image. And the total savings is somewhere about a whooping 53%.

Note: There are other image compression APIs/tools which I haven’t tried yet. Android build system itself has a pngcrusher which tries to optimize all your assets.

Windows 10 Review – First impressions

This slideshow requires JavaScript.

I have a laptop, 1 year old, HP Pavillion, with an Intel i5 processor (quite a recent one), with 8 GB RAM. It used to run Windows 7 before. And it was slow. I kept it as a backup incase my MacBook Pro failed (it never has). Well, I also have an octa-core AMD desktop running on Windows 8.1, which again is a backup when the first backup fails, or frustrates me enough.

The laptop, running Windows 7 was too slow to even use it just for browsing. And I tried all tricks to make it faster. Even then, it was only marginally better, which wasn’t good enough. So, I rarely used the laptop. Today, it prompted me that Windows 10 has arrived. I thought to myself, in worst case scenario, after the upgrade, I might stop using this laptop, and I pressed the “Download” button, anxiously. After a good 1 hour, it was up and running Windows 10. Read on for Windows 10 review and my first impressions.

The upgrade process was rather smooth. No problems at all.  All hardware seem to be working fine. And it didn’t lose any of my data, even my Wi-Fi passwords. All applications I checked worked fine.

After logging in for the first time, it wasn’t any better than Windows 7. I was just browsing through all parts of the OS, restarted it a few times, and it started showing signs of improvement.

Windows 10 promises very fast boot times. On this laptop, I didn’t notice much of an improvement than Windows 7. I did try to restart it a few times, but the boot time, and the logging in time seemed to take longer than I would have liked. From powering it on to getting to see the desktop, it was a good 3 minutes on this machine.

Interface: The whole UI has been refreshed, but I think, it could have been a little better. For now I am using the default theme. May be I should try a different theme. At first, you might get a little confused. As with all major OS upgrades, I shouldn’t be complaining about this.

  • The PIN login is something new on a desktop OS. You can enable this (4-digit PIN), after you verify your account ( or with which you would be logging in to the system.
  • The window styles have been refreshed with a minimalistic and less colorful theme (default).
  • The Task Bar, Start buttons are a welcome change from Windows 8. People coming from Windows 7 might be a tad more comfortable than those upgrading from Windows 8.
  • There are no hidden slide outs from all sides (as in Windows 8).
  • The controls at the right bottom corner of the Task bar looks nice.
  • Almost every icon on Windows 10 is more 3D-ish. You might find it a bit difficult at recognizing the familiar icons.
  • The start menu is definitely more cluttered than Windows 7, but a lot better than how it worked on Windows 8.
  • The new Task View is nice. I really liked it. It’s easier than ever to work with multiple desktops on Windows finally.

Coming to performance, for now, I am quite surprised with it. It’s fast and snappy as compared to Windows 7. Apart from the boot times, everything else starts up faster than before, and suddenly makes me feel using a brand new laptop. I am glad that I upgraded. Soon, I would be upgrading my desktop to Windows 10, but unfortunately, I have got no notification there. Probably because, that’s a custom built machine (AMD), and it would take some more time for Microsoft to support all kinds of hardware to ensure a smooth upgrade.

Microsoft Edge: The new browser is hell of an upgrade from the aeging Internet Explorer. Now, you won’t bang your head if you accidentally click on the browser from Microsoft. It starts up very very fast, and responds to clicks just like any other browser. Although I haven’t used it extensively, it’s definitely an welcome upgrade from IE. Bye Bye IE.

Cortana: Well, again, we have to see how it works, but at least, they made a start. The concept is like Google Now on Android and Apple’s Proactive Assistance. On the face of it, it seems very useful.

These are my first impressions of Windows 10 running on my laptop. So far, I would give it a thumbs up. However, I would probably wait for some more weeks before I give my final verdict.

PIN Screen Library for Android

Here’s a simple and easy implementation for adding PIN lock support for your Android apps. The library can be themed with your app theme colors. It has smooth animations and vibrate cues when something’s wrong.


To use this, you just have to write about 10 lines of code, including layouts and everything. It’s so simple.

Steps to implement

  • Add as dependency



Android Studio:

compile ‘com.kbeanie:pinscreenlibrary:1.0.0@aar’ 

  • Add PinView to your layout file.

android:layout_height=”wrap_content” />

  • Initialize PinView in two modes


And finally, handle the callbacks for all events. The complete source code for this project can be found on Github. Let me know if you have any queries or issues. I would be happy to resolve them.

New Sensors in Android 5.0 (Lollipop)

I found a few non-documented sensors on Lollipop (Android 5.0) on a Nexus 5.

Tried looking for them, but could’t find any documentation on them on. Looks like they are mostly software sensors.

Android app on Google PlayHere’s the list that I have found. I have written a simple app that displays all the sensors and shows there values. Not to mention that, only for the ones that are documented. For the ones not documented, you can only see it’s details.

There are only a few which appear on
Here’s an excerpt from the announcement.

New types of sensors

In Android 5.0, a new tilt detector sensor helps improve activity recognition on supported devices, and a heart rate sensor reports the heart rate of the person touching the device.
New interaction composite sensors are now available to detect special interactions such as a wake up gesture, a pick upgesture, and a glance gesture.

Since there’s no documentation, for some of them, I don’t really know what they do. The only thing that gives you a clue is the sensor name. And, I don’t know how to work with them as well.

Tilt Detector

AMD Sensor (No idea, whats that)

RMD Sensor (No idea, whats that)

Basic Gestures Sensor

Tap Sensor

Facing Sensor

Tilt Sensor


Pedestrian Activity Monitor

Update: Seems, the documentation is coming soon. XDA picked this up.