在android webview上启用输入文件摄像头 [英] Enable input file camera on android webview

查看:31
本文介绍了在android webview上启用输入文件摄像头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建Web视图,以加载外部网站.

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.

似乎Webview有一些阻碍.该如何解决?

It looks like the webview has some blocking. How to fix this?

<?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);
    }
}

推荐答案

首先,您应该在HTML文件中添加以下代码

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>

在Android端创建一个类后

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()
    }
}

并向您的Webview添加WebAppInterface处理程序

and add WebAppInterface handler to you webview

webView.addJavascriptInterface(WebAppInterface(this), "Android")

例如

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()
    }
}

有关更多信息,请: Android Webview

这篇关于在android webview上启用输入文件摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆