【问题标题】:Opening camera from android webView从android webView打开相机
【发布时间】:2017-05-24 01:34:45
【问题描述】:

我正在尝试从加载在 android webView 中的 html 页面打开 android 原生相机 通过使用 HTML 输入类型文件标记。

<input type="file" accept="image/*">

我不知道为什么,但相机没有打开,我不知道该怎么办。

我已经在 iPhone webView 上尝试过相同的页面,并且它可以正常工作。

我能做什么?

【问题讨论】:

  • 如果我错了,请纠正我,但您尝试从 html 页面打开相机正确吗?

标签: javascript android html webview


【解决方案1】:

如果我正确理解你的问题

您想通过点击网页中的按钮(html)来打开安卓设备的摄像头吗?

基于这个假设, 您需要执行以下操作

使用 JavascriptInterface

public class WebVCamBridgeInterface {
        /**
         * Javacript function to start native camera
         */
        @JavascriptInterface
        public void takePicture() {
            captureImage();
        }

        /**
         * Javascript function to start the GalleryActivity for user to choose the  image to be uploaded
         */
        @JavascriptInterface
        public void showPictures() {
            Intent intent = new Intent(LandingActivity.this, GalleryActivity.class);
            startActivityForResult(intent, Constants.REQ_GALLERY);
        }

    }

将 JSinterface 添加到您的 web 视图中

webView.addJavascriptInterface(new WebVCamBridgeInterface (), "AndroidDevice");

在你的 html/web 页面中有以下 JS

<script>
  function takePicture() {
    if(typeof AndroidDevice !== "undefined"){
      AndroidDevice.takePicture();
    }
  }

  function showPictures() {
    if(typeof AndroidDevice !== "undefined"){
      AndroidDevice.showPictures();
    }
  }

  function imageData(data){
    document.getElementById('displayImage').setAttribute( 'src', 'data:image/png;base64,'+data );
    if(typeof AndroidDevice !== "undefined"){
    }
  }
</script>

我提供了一个带有演示视频的示例项目的链接,看看。 https://drive.google.com/drive/folders/0BwRMp8dK9LMLeEo5cTlXVE9ZUW8?resourcekey=0-6dEjytPymBZvebmmyy9ymQ&usp=sharing

你也可以参考这些教程

干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    相关资源
    最近更新 更多