【问题标题】:Phonegap build - Uncaught TypeError: Cannot read property 'getPicture' of undefinedPhonegap 构建 - 未捕获的类型错误:无法读取未定义的属性“getPicture”
【发布时间】:2015-12-08 14:16:44
【问题描述】:

应用程序

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

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

问题

当我点击按钮触发摄像头时,我收到控制台错误:

Uncaught TypeError: Cannot read property 'getPicture' of undefined

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,但问题完全相同。

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

【问题讨论】:

  • 所有适当的权限和功能都到位了吗?检查this question的答案

标签: javascript cordova phonegap-build


【解决方案1】:

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

确保将其添加到您的 config.xml 文件中:

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

据我所知,这基本上创建了 navigator 对象

【讨论】:

    猜你喜欢
    • 2014-08-29
    • 2021-12-22
    • 2015-01-06
    • 2017-07-26
    • 2019-02-26
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多