如何从 webview 中的按钮启动相机 -Android

Posted

技术标签:

【中文标题】如何从 webview 中的按钮启动相机 -Android【英文标题】:How to launch camera from button in webview -Android 【发布时间】:2014-03-07 16:13:25 【问题描述】:

如何通过 Web 视图中的按钮启动设备的摄像头?

我在 android 中有这段代码:

protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview = (WebView)findViewById(R.id.webView1);
        webview.getSettings().setjavascriptEnabled(true);
        webview.loadUrl("file:///android_asset/camtest.html");

而且Html文件很简单:

<html>
    <body>
        <form enctype="multipart/form-data" method="post">    
              <h2>capture=camera</h2>  
              <input type="file" accept="image/*;capture=camera"></input>
        </form>
    </body>
</html>

当我启动应用程序并单击按钮时,

日志显示:

02-10 12:00:03.775: V/WebViewInputDispatcher(10110): blockWebkitDraw
02-10 12:00:03.775: V/WebViewInputDispatcher(10110): blockWebkitDraw lockedfalse
02-10 12:00:04.085: D/webview(10110): blockWebkitViewMessage= false

而且相机没有启动。

我错过了什么?

【问题讨论】:

通过 html 无法启动相机。您必须使用 phonegap lib 或类似的东西。或者您必须编写自己的 JavascriptInterface 来访问相机。 @Shudy 【参考方案1】:

你可以使用Phonegap

将活动扩展到DroidGap

然后在assets folder 内创建文件夹www 并在文件夹www 内创建文件夹js 并在其中放置phonegap.js

然后创建文件index.html:

<!DOCTYPE html>
<html>
  <head>
    <title>Capture Photo</title>
    <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"/>
    <script type="text/javascript" charset="utf-8" src="js/phonegap.js"></script>
    <script type="text/javascript" charset="utf-8">

    var pictureSource;   // picture source
    var destinationType; // sets the format of returned value 

    // Wait for PhoneGap to connect with the device
    //
    document.addEventListener("deviceready",onDeviceReady,false);

    // PhoneGap is ready to be used!
    //
    function onDeviceReady() 
        pictureSource=navigator.camera.PictureSourceType;
        destinationType=navigator.camera.DestinationType;
    

    // Called when a photo is successfully retrieved
    //
    function onPhotoDataSuccess(imageData) 
      // Get image handle
      //
      var smallImage = document.getElementById('smallImage');

      // Unhide image elements
      //
      smallImage.style.display = 'block';

      // Show the captured photo
      // The inline CSS rules are used to resize the image
      //
      smallImage.src = "data:image/jpeg;base64," + imageData;
    

    // Called when a photo is successfully retrieved
    //
    function onPhotoFileSuccess(imageData) 
      // Get image handle
      console.log(JSON.stringify(imageData));

      // Get image handle
      //
      var smallImage = document.getElementById('smallImage');

      // Unhide image elements
      //
      smallImage.style.display = 'block';

      // Show the captured photo
      // The inline CSS rules are used to resize the image
      //
      smallImage.src = imageData;
    

    // Called when a photo is successfully retrieved
    //
    function onPhotoURISuccess(imageURI) 
      // Uncomment to view the image file URI 
      // console.log(imageURI);

      // Get image handle
      //
      var largeImage = document.getElementById('largeImage');

      // Unhide image elements
      //
      largeImage.style.display = 'block';

      // Show the captured photo
      // The inline CSS rules are used to resize the image
      //
      largeImage.src = imageURI;
    

    // A button will call this function
    //
    function capturePhotoWithData() 
      // Take picture using device camera and retrieve image as base64-encoded string
      navigator.camera.getPicture(onPhotoDataSuccess, onFail,  quality: 50 );
    

    function capturePhotoWithFile() 
        navigator.camera.getPicture(onPhotoFileSuccess, onFail,  quality: 50, destinationType: Camera.DestinationType.FILE_URI );
    

    // A button will call this function
    //
    function getPhoto(source) 
      // Retrieve image file location from specified source
      navigator.camera.getPicture(onPhotoURISuccess, onFail,  quality: 50, 
        destinationType: destinationType.FILE_URI,
        sourceType: source );
    

    // Called if something bad happens.
    // 
    function onFail(message) 
      alert('Failed because: ' + message);
    

    </script>
  </head>
  <body>
    <button onclick="capturePhotoWithData();">Capture Photo With Image Data</button> <br>
    <button onclick="capturePhotoWithFile();">Capture Photo With Image File URI</button> <br>
    <button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
    <button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br>
    <img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
    <img style="display:none;" id="largeImage" src="" />
  </body>
</html>

然后你必须添加库cordova.jar

然后运行你的应用程序

【讨论】:

在哪里可以找到“cordova.jar”文件?

以上是关于如何从 webview 中的按钮启动相机 -Android的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Webview 中访问相机?

如何从 Webview 中访问相机?

如何在android webview中打开相机?

无法通过 react-native-webview 访问相机

在 webview 中从相机上传图像不起作用

如何在本机反应中从相机按钮切换到视频录制按钮