Scenarios
When the default file-selection behavior of the <input type="file"/> tag in an HTML5 offline package does not meet your needs, use MPH5OpenFileChooserProvider to take over image selection. This lets you customize the selection UI, support multiple selections, and handle dynamic permissions.
Implementation
-
Register
MPH5OpenFileChooserProviderin the callback after mPaaS initialization is complete.Pass a
MPH5OpenFileChooserProviderinstance toH5Utils.setProvider. The instance overrides two methods:needIntercept: Controls whether your custom implementation takes over. Returntrueto intercept the HTML5 container and use your custom logic; returnfalseto fall through to the default behavior.-
openFileChooser: Contains your custom image-selection logic. This method runs only whenneedInterceptreturnstrue.H5Utils.setProvider(MPH5OpenFileChooserProvider.class.getName(), new MPH5OpenFileChooserProvider() { @Override public boolean needIntercept(Activity activity, ValueCallback valueCallback, boolean b, APFileChooserParams apFileChooserParams) { // If the custom image selection feature is not needed, return false to avoid intercepting the HTML5 container. By default, the container is not intercepted. // To use the custom image selection feature, intercept the HTML5 container and return true. return true; } @Override public void openFileChooser(Activity activity, ValueCallback valueCallback, boolean b, APFileChooserParams apFileChooserParams) { // Get a valid URI and pass it to the frontend. // This method must be called. Otherwise, the next click callback will not be executed. // 1. Use an intent to open an Activity that calls the system album or camera. // 2. Get the selected image. // 3. Close the page and pass the image to the frontend through valueCallback. valueCallback.onReceiveValue(xx); } });ImportantAlways call
valueCallback.onReceiveValue(xx)before closing the Activity, even if the user cancels. If this call is skipped, the HTML5 container suppresses subsequent click events on the same<input>element.
-
Implement your image-selection logic inside the
openFileChoosercallback:Use an
Intentto launch a customActivitythat opens the system photo album or camera.Retrieve the selected image from the result.
Pass the image URI to the frontend via
valueCallback, then close theActivity.
Reference solutions
Both solutions implement the same three-phase flow: launch a selection Activity → get the result → call valueCallback.onReceiveValue(xx) to pass the data back. They differ in how the result travels from the selection Activity to the openFileChooser callback.
Solution 1
Use a broadcast to decouple the selection Activity from the openFileChooser callback.
In the
openFileChoosercallback, register a broadcast receiver that waits for the selection result.In the
openFileChoosermethod, use anIntentto navigate to a customActivityfor image selection.Call the system camera or photo-album SDK. Add optional extensions, such as image cropping.
After selection, send a broadcast from the selection
Activityto the registered receiver, then close theActivity. The broadcast carries the result back to theopenFileChoosercallback context.In the broadcast receiver, call
valueCallback.onReceiveValue(xx)on theWebViewto return the selected file to the frontend.
Solution 2
Pass valueCallback directly to the selection Activity and call it there.
In the
openFileChoosermethod, use anIntentto navigate to a customActivity, passing thevalueCallbackto thatActivityas an extra.Call the system camera or photo-album SDK. Add optional extensions, such as image cropping.
Call
valueCallback.onReceiveValue(xx)on theWebViewto return the selected file to the frontend.Close the
Activity.