Mike Purcell Mike Purcell - 3 months ago 49x
iOS Question

Webview - Best way to upload images from device

I currently have apps on google play and itunes, which are native wrappers to a shared webview. The beauty of the webview is I can make changes on the webview side and they will immediately be viewable on any device, however I am having issues with being able to upload images from user's albums/galleries stored on their device.

What, if any, is the fastest implementation, using my current webview strategy, to allow users to upload images from their device's native albums/galleries?


You can use the standard file input type:

<form action="/server/upload" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">

This should work out of the box on iOS6+ and bring up native open file dialog, but you will have to do some coding on Android.

On olders platforms there is a workaround to use unofficial api (WebChromeClient #openFileChooser) but Google broke this in KitKat and then released official support for this feature in Lollipop with WebChromeClient.html#onShowFileChooser. So, KitKat will probably remain broken, but on other platforms it should work fine.

Please see this Android bug report Issue 62220.

You can implement this yourself (look at this example), but if you're looking for a WebView subclass that "just works", you may want to try Android-AdvancedWebView project.

Another option, or just as a fix for KitKat devices, is to have the native wrapper encode the file as Base64 String and then the HTML app could send it to the server.