Webview和相机访问

时间:2018-04-17 10:51:22

标签: java android android-studio webview

我有一个web app(通过SSL保护),可以访问camera。它会在camera中显示canvas次抓取。它在浏览器中完美运行。但是,我希望camera内容显示在我的应用中的Webview中。 这是方法onCreate中的代码:

    final WebView myWebView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.getSettings().setAllowFileAccessFromFileURLs(true);
    myWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    myWebView.loadUrl("https://myvideoapp.me");
    myWebView.setWebViewClient(new WebViewClient());

这是我在manifest.XML中使用的权限

 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

1 个答案:

答案 0 :(得分:0)

如果您正在编写android +4.4

,则需要获得许可

如果您想要获得CAMERA权限,请在您的班级内的MainActivity中使用此项:

private static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 0;


private boolean checkAndRequestPermissions() {

    int permission = ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA);

    List listPermissionsNeeded = new ArrayList();

    if (permission != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(android.Manifest.permission.CAMERA);

    }
    if (!listPermissionsNeeded.isEmpty()) {
        ActivityCompat.requestPermissions(this, (String[])     listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),     REQUEST_ID_MULTIPLE_PERMISSIONS);

        return false;
    }

    return true;
}

而且你必须在你的onCreate void函数中检查它,如下所示:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    if (!checkAndRequestPermissions()) {
        return; 
    }

.........