title: I want to use the getUserMedia in Android Lollipop 5.0 WebView
body: <p>I want to start a web camera using the getUserMedia in WebView of Android.
  The following has written the code, but, errorCallback will have is called.
  why not?</p>
  
  <p>Incidentally, The following web site was implemented by reference to.</p>
  
  <blockquote>
    <p><a href="http://www.flapjacksandcode.com/blog/2015/2/17/android-webviews-getusermedia-putting-it-all-together" rel="nofollow">Android, WebViews, &amp; getUserMedia: Putting it all together</a></p>
  </blockquote>
  
  <pre><code>myWebView = (WebView) findViewById(R.id.webview);
  
  // Settings
  WebSettings webSettings = myWebView.getSettings();
  webSettings.setJavaScriptEnabled(true);
  webSettings.setAllowFileAccessFromFileURLs(true);
  webSettings.setAllowUniversalAccessFromFileURLs(true);
  myWebView.addJavascriptInterface(new WebAppInterface(this), “Android”);
  
  // Set a web view client and a chrome client
  myWebView.setWebViewClient(new WebViewClient());
  myWebView.setWebChromeClient(new WebChromeClient() {
    // Need to accept permissions to use the camera and audio
    @Override
    public void onPermissionRequest(final PermissionRequest request) {
      Log.d(TAG, “onPermissionRequest”);
      MainActivity.this.runOnUiThread(new Runnable() {
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void run() {
          if(request.getOrigin().toString().equals(LOCAL_FILE)) {
            request.grant(request.getResources());
          } 
          else {
              request.deny();
          }
        }
      });
    }
  });
  // Load the local HTML file into the webview
  myWebView.loadUrl(LOCAL_FILE);
  </code></pre>
  
  <p>[permissions]</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.RECORD_AUDIO" /&gt;
  &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
  &lt;uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /&gt;
  </code></pre>
  
  <p>[JavaScript]</p>
  
  <pre><code> var constraints = {video: true, audio: true};
   var localStream = null;
   function init() {
      console.log(‘Getting user media with constraints’, constraints);
      navigator.webkitGetUserMedia(constraints, handleUserMediaSuccess, handleUserMediaError);
   };
  
   function handleUserMediaSuccess(stream) {
      console.log(‘[+] Successfully got local video stream!’);
      attachMediaStream = function(element, stream) {
            element.src = window.webkitURL.createObjectURL(stream);
      };
      attachMediaStream(document.getElementById(“localVideo”), stream);
   };
  
   function handleUserMediaError(error) {
      alert(‘[!] getUserMedia error: ‘, error);
   };
  
   window.onload = function() {
      init();
   };
  </code></pre>