简体   繁体   English

Phonegap构建-未捕获的TypeError:无法读取未定义的属性'getPicture'

[英]Phonegap build - Uncaught TypeError: Cannot read property 'getPicture' of undefined

THE APP APP

I'm trying to create a simple app which takes a picture, am testing phonegap build 我正在尝试创建一个带有照片的简单应用,正在测试phonegap版本

When the app loads I get a console log saying deviceready - which is logged when the device is ready 应用加载后,我会收到一个控制台日志,上面写着deviceready设备准备就绪时会记录该日志

THE PROBLEM 问题

When I click the button to fire the camera, I get a console error: 当我单击按钮启动相机时,出现控制台错误:

Uncaught TypeError: Cannot read property 'getPicture' of undefined

JS / HTML 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>

I also tried with cordova.js but same issue exactly. 我也尝试了cordova.jscordova.js同样的问题。

I've seen this issue mentioned before but cannot find a fix for it, is there one? 我已经看过前面提到的这个问题,但找不到解决方法,有没有? Is there a better way to do this? 有一个更好的方法吗?

Figured it out although can't find this documented anywhere! 想通了,尽管找不到任何地方记录在案!

Make sure you add this to your config.xml file: 确保将其添加到config.xml文件中:

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

This basically, from what I can gather, creates the navigator object 根据我的收集,这基本上可以创建navigator对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM