Webview,使用html5访问摄像头

时间:2019-05-14 17:10:52

标签: android webview

  

使用google翻译器进行书写。

我正在使用Webview访问我的应用程序。

我需要使用相机拍照,但无法启用。

我的应用程序位于URL中,可通过Webview进行访问。

查看我的代码:

Manifest.xml

<uses-permission android:name="android.permission.CAMERA" />

MainActivity

package br.com.abmprotege;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.net.Uri;
import android.content.Intent;


public class MainActivity extends AppCompatActivity {

    String url = "https://link-my-app";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Verifica se existe a perssão para usar a camera
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
           ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},0);
        }



        WebView myWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        //myWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

        myWebView.loadUrl(url);


        myWebView.setWebViewClient(new MyWebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
        });


    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals(url)) {
                return false;

            }

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }


}

我需要做些什么才能能够通过网络视图从手机访问相机?

非常感谢您。

1 个答案:

答案 0 :(得分:0)

您需要覆盖 WebChromeClient 类的 onShowFileChooser 方法。

/**
 * Tell the client to show a file chooser.
 *
 * This is called to handle HTML forms with 'file' input type, in response to the
 * user pressing the "Select File" button.
 * To cancel the request, call <code>filePathCallback.onReceiveValue(null)</code> and
 * return {@code true}.
 *
 * @param webView The WebView instance that is initiating the request.
 * @param filePathCallback Invoke this callback to supply the list of paths to files to upload,
 *                         or {@code null} to cancel. Must only be called if the
 *                         {@link #onShowFileChooser} implementation returns {@code true}.
 * @param fileChooserParams Describes the mode of file chooser to be opened, and options to be
 *                          used with it.
 * @return {@code true} if filePathCallback will be invoked, {@code false} to use default
 *         handling.
 *
 * @see FileChooserParams
 */
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
        FileChooserParams fileChooserParams) {
    return false;
}

Github上的工作示例