Solution 1 :
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
Problem :
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 happen.
It looks like the webview has some blocking. How to fix this?
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns_android="http://schemas.android.com/apk/res/android"
package="br.com.app...">
<uses-permission android_name="android.permission.INTERNET"/>
<uses-permission android_name="android.permission.CAMERA" />
<uses-permission android_name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android_name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android_allowBackup="true"
android_icon="@mipmap/ic_launcher"
android_label="@string/app_name"
android_roundIcon="@mipmap/ic_launcher_round"
android_supportsRtl="true"
android_theme="@style/AppTheme">
<activity android_name=".MainActivity">
<intent-filter>
<action android_name="android.intent.action.MAIN" />
<category android_name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package br.com.app...;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
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);
}
}
Comments
Comment posted by Tiago
Hello good day. I have some doubts. The code
READ [ANSWERED] android - Crashlytics: Is it possible to obfuscate native libraries and yet obtain symbolicated crash reports?
Powered by Inline Related Posts
Comment posted by Nikhil Vadoliya
[1] Yes, and call onClick as per the above way. [2] Yes, As per you code you should be add-in MainActivity.