javascript - Phonegap 构建 - 未捕获的类型错误 : Cannot read property 'getPicture' of undefined

标签 javascript cordova phonegap-build

应用程序

我正在尝试创建一个简单的拍照应用,正在测试 phonegap 构建

当应用程序加载时,我得到一个控制台日志,上面写着 deviceready - 当设备准备就绪时记录

问题

当我点击按钮启动相机时,出现控制台错误:

未捕获的类型错误:无法读取未定义的属性“getPicture”

JS/HTML

<button onclick="app.takePicture();">Take Picture</button>

<script type="text/javascript" src="phonegap.js"></script>
<script>
var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id); // <-- this works ok
    },

    takePicture: function() {
      navigator.camera.getPicture( function( imageURI ) {
        alert( imageURI );
      },
      function( message ) {
        alert( message );
      },
      {
        quality: 50,
        destinationType: Camera.DestinationType.FILE_URI
      });
    }
};

app.initialize();
</script>

我也尝试过使用 cordova.js 但同样的问题。

我之前看到过这个问题,但找不到解决方法,有吗?有更好的方法吗?

最佳答案

想通了,虽然在任何地方都找不到这个记录!

确保将此添加到 config.xml 文件中:

<plugin name="org.apache.cordova.camera" spec="0.3.6" source="pgb" />

根据我的收集,这基本上创建了 navigator 对象

关于javascript - Phonegap 构建 - 未捕获的类型错误 : Cannot read property 'getPicture' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34158171/

相关文章:

javascript - Express JS 通过控制台日志查看

javascript - 如何检测字符串的特定范围是否在换行符中?

cordova - 如何向 ionic 推送通知添加操作按钮?

iphone - 以编程方式编辑 iOS 联系人

ios - 捕获并存储用相机拍摄的照片到本地数据库/PhoneGap/Cordova/iOS

javascript - 正则表达式匹配额外不需要的内容

android - 我在Ionic 5中遇到Gradlew软件包错误

javascript - Phonegap 下载/解压缩 mp3 播放问题

css - 移动应用程序 anchor 链接跳转到固定标题下

javascript - 在以 POST(而不是 GET)方式发送请求后,如何将对象作为 html 页面插入到来自后端的 html 元素中?