Unfortunately, as a user of Slartibartfast said in one of the comments on your question, the input type “file” will not work on 4.4 devices. There is an issue in the google code that says this is the alleged behavior (Status: WorkingAsIntended).
However, the code you provided works in 4.4.4 (tested on a Nexus 7 tablet) and on <Version 4.4 os.
In 5.0, they added a documented method . I am using the following code:
mWebView.setWebChromeClient(new WebChromeClient() { // For Android < 3.0 - undocumented method @SuppressWarnings("unused") public void openFileChooser( ValueCallback<Uri> uploadMsg ) { openFileChooser( uploadMsg, "" ); } // For Android 3.0+ - undocumented method public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType ) { mUploadCallback = uploadMsg; openFileChooserActivity(); } // For Android > 4.1 - undocumented method @SuppressWarnings("unused") public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){ openFileChooser( uploadMsg, "" ); } // For Android > 5.0 public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { mUploadCallbackLollipop = filePathCallback; openFileChooserActivity(); return true; } });
Note the changed callback parameter for> 5.0: from ValueCallback<Uri> to ValueCallback<Uri[]> .
So, with> = 4.4 and <4.4.4, you must implement your own file upload mechanism. Some suggestions:
- check with javascript the version of android os and specify
<input type="file"> where it works. In versions 4.4, create an html element with your own URL scheme (as Sishant said, for example, <form action="image://choose"> ). Then you can catch it in shouldOverrideUrlLoading and call the javascript function from java using the file path, but you will not be able to access the contents of your file. For small files, you can pass the contents of the file to base64 as a parameter to the javascript function, but for larger files, you probably get an OutOfMemory exception. - you can follow the steps above but handle the file upload in java and provide only the path to the javascript function. The submit button can use what Amrut Bidri said to trigger a download in java.
So, as an output, I see two options for os versions between 4.4 and 4.4.4: either use javascript to load the file (memory problems), or load it into java and implement your own communication mechanism between your application and WebView. For them, you must have access to the webpage that you load in the webview.
I used the javascript download method as we use small files and it was a little faster to implement:
public boolean shouldOverrideUrlLoading(WebView view, String url) { ... else if (url.startsWith(UPLOAD_FILE_URL_PREFIX)) { openFileChooserActivity(); return true; } return false; }
As a result of the action, I do something similar for versions 4.4 (the javascript insertFileForUpload function handles the file upload):
private void invokeJavascriptWithFileContent(Uri fileUri) { String fileContentInBase64 = readAndConvertFileToBase64(fileUri); if (!fileContentInBase64.isEmpty()) { String fileMimeType = getMimeType(activity, fileUri); String fileName = getFileName(activity, fileUri);
Also, with regard to https://github.com/delight-im/Android-AdvancedWebView , it seems that downloading files works, but they say: "Files download automatically (check availability with AdvancedWebView.isFileUploadAvailable ()) 'and' isFileUploadAvailable 'contains the following code:
public static boolean isFileUploadAvailable() { return isFileUploadAvailable(false); } public static boolean isFileUploadAvailable(final boolean needsCorrectMimeType) { if (Build.VERSION.SDK_INT == 19) { final String platformVersion = (Build.VERSION.RELEASE == null) ? "" : Build.VERSION.RELEASE; return !needsCorrectMimeType && (platformVersion.startsWith("4.4.3") || platformVersion.startsWith("4.4.4")); } else { return true; } }
This way you may have the same problems with normal web browsing, but I have not tested the library, so I cannot say.
I may have created a mess of response, but hope you find something useful.