I have been struggling to upload files from WebView since last few days and there is no progress. I googled and implemented all suggested solutions but none works, like: solutions suggested here, and so on.
Problem: I have a HTML page with the following code to upload a file. It works fine in a desktop browser like Firefox and built-in browser of emulator / AVD i.e., when I click "Browse..." button rendered by element, browser opens a Dialog box where I can choose a file to upload.
However, in the android 3.0 emulator / AVD, when I click on "Choose file", nothing happens, no file dialog is opened!!!
<form method="POST" enctype="multipart/form-data">
File to upload: <input type="file" name="uploadfile">
<input type="submit" value="Press to Upload..."> to upload the file!
</form>
Could anyone please suggest a possible solution at the earliest.
Working Method from HONEYCOMB (API 11) to Marshmallow (API 23)
Modified
onActivityResult()
Now in
onCreate()
oronCreateView()
paste the following codethis is the only solution that i found that works!
In 5.0 Lollipop, Google added an official method, WebChromeClient.onShowFileChooser. They even provide a way to automatically generate the file chooser intent so that it uses the input accept mime types.
For Android versions before KitKat, the private methods mentioned in the other answers work. I have not found a good workaround for KitKat (4.4).
Found a SOLUTION which works for me! Add one more rule in the file
proguard-android.txt
:This is a full solution for all android versions, I had a hard time with this too.
Also I want to add that the "upload page" like the one in this example, wont work on < 4 versions, since it has an image preview feature, if you want to make it work use a simple php upload without preview.
Update:
Please find the solution for lollipop devices here and thanks for gauntface
Update 2:
Complete solution for all android devices till oreo here and this is more advanced version, you should look into it, maybe it can help.
Webview - Single & Multiple files choose
you needs two minutes to implement this code:
build.gradle
java code: