WebView,使用HTML5访问摄像头



使用Google Translator编写。

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

我需要访问相机拍照,但我无法启用它。

我的应用程序在URL中,在其中访问WebView。

请参阅我的代码:

subest.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;
        }
    }

}

我需要做什么才能通过WebView从手机访问相机?

非常感谢。

您需要覆盖 onshowfilechooser web Chromeclient class> class。

的方法
/**
 * 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的工作示例

最新更新