File Handling
Smart WebView provides robust support for handling file uploads initiated from your web content, including direct access to the device camera, and manages file downloads.
File Uploads & Camera Access
This functionality allows users to interact with <input type="file"> elements in your web content.
Configuration:
Controlled by variables in SmartWebView.java:
ASWP_FUPLOAD(true/false): Globally enable/disable file input.ASWP_CAMUPLOAD(true/false): Include a camera capture option in the chooser.ASWP_MULFILE(true/false): Allow multiple file selection if the HTML input tag supports it.
Permissions:
The following permissions are declared in AndroidManifest.xml and requested at runtime if needed:
android.permission.CAMERAandroid.permission.READ_MEDIA_IMAGESandroid.permission.READ_MEDIA_VIDEOandroid.permission.READ_MEDIA_AUDIOandroid.permission.WRITE_EXTERNAL_STORAGE(withmaxSdkVersionfor older Android versions)
How it Works:
- A user taps an
<input type="file">element in the WebView. - The
onShowFileChoosermethod inFileProcessing.javais triggered. - It constructs an
Intentthat opens a system chooser, allowing the user to select files or use the camera (if enabled). - The HTML
acceptattribute can filter the file types shown (e.g.,image/*). - The HTML
multipleattribute, combined withASWP_MULFILE, allows for multi-file selection. - The selected file URIs are returned to the WebView to be processed by your web application.
Downloads
This handles files downloaded from the WebView.
How it Works:
- The WebView’s
DownloadListenerdetects a URL that triggers a download. - It uses the Android
DownloadManagerservice to handle the download. - A system notification shows the download progress.
- Files are saved to the public “Downloads” directory on the device.
- A Toast message confirms that the download has started.