我为我的应用程序安装了Cordova相机插件。当我运行我的计算机上的index.html时,它表示'TypeError:navigator.camera未定义',当我运行Camera功能时。这可能(我不知道)发生,因为该插件只适用于手机。然后,我构建了Cordova应用程序,没有出错......并将其安装在我的手机上。当我点击'画廊'按钮时,现在他们也没有任何事情发生。Cordova:相机PLugin不工作
我的代码
<body>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<input type="button" value="Gallery" onclick="getPhoto()"/>
<img id="myImage" src="#" />
<script>
function getPhoto() {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI});
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
}
function onFail(message) {
alert('Failed because: ' + message);
}
}
</script>
</body>
这些都是我做的所有步骤(忘了吗?):
- 创造新的科尔多瓦项目
- 添加摄像头插件:科尔多瓦插件添加科尔多瓦 - 插件相机
在platform/android/Manifest.xml中添加权限:
使用权限android:name =“android.permission.CAMERA”/ uses-feature android:name =“android.hardware.camera”/ uses-feature android:name =“android.hardware.camera.autofocus”/
写我刚刚发布
- 构建应用程序
难道我忘了什么事或为什么会出错的代码?
是的,你忘了一些东西:请在编码之前阅读文档。您必须先等待设备准备就绪事件,然后才能在应用中使用相机。 https://www.npmjs.com/package/cordova-plugin-camera – Joerg