THE APP
我试图创建一个简单的应用程序拍照,我正在测试手机版本
当应用程序加载时,我会收到一个控制台日志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
但同样的问题。
我之前看到过这个问题,但无法找到解决方法,有吗?有更好的方法吗?
答案 0 :(得分:2)
想出来虽然无法在任何地方找到这个记录!
请务必将其添加到config.xml
文件中:
<plugin name="org.apache.cordova.camera" spec="0.3.6" source="pgb" />
基本上,根据我的收集,创建navigator
对象