我爲我的應用程序安裝了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