Enable input file camera on android webview

后端 未结 1 877
忘了有多久
忘了有多久 2020-12-07 05:12

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

相关标签:
1条回答
  • 2020-12-07 06:07

    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

    0 讨论(0)
提交回复
热议问题