I am creating a webview, to load an external website.
On this site I have a form with an input file. Clicking it should open the cell phone\'s camera, but it doesn\'t
First, you should add below code in your HTML file
<input type="button" value="Say hello" onClick="showAndroidCamera()" />
<script type="text/javascript">
function showAndroidCamera() {
Android.openCamera();
}
</script>
After creating one class in the Android side
class WebAppInterface(private val mContext: Context) {
/** Show a toast from the web page */
@JavascriptInterface
fun openCamera() {
//This method called when user clicks on webview button.
Toast.makeText(mContext, open Camera, Toast.LENGTH_SHORT).show()
}
}
and add WebAppInterface handler to you webview
webView.addJavascriptInterface(WebAppInterface(this), "Android")
For Example
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.getSupportActionBar().hide();
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://apps.url/loja/index.php");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(WebAppInterface(this), "Android")
}
}
class WebAppInterface(private val mContext: Context) {
/** Show a toast from the web page */
@JavascriptInterface
fun openCamera() {
//This method called when user clicks on webview button.
Toast.makeText(mContext, open Camera, Toast.LENGTH_SHORT).show()
}
}
For more information please : Android Webview