0
votes

I installed the Cordova camera plugin for my App. When I am running the index.html on my Computer it says 'TypeError: navigator.camera is undefined' when I am running the Camera function. This MAY (i dont know) happens because the Plugin just works on the the mobile. Then I build the Cordova App, nothing went wrong... and installed it on my phone. Now thers also nothing happening when I click on th 'Gallery'-Button.

MY CODE

    <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>

These are all steps I did (forgot one?):

  1. create new cordova project
  2. add Camera Plugin: cordova plugin add cordova-plugin-camera
  3. add permissions in platform/android/Manifest.xml:

    uses-permission android:name="android.permission.CAMERA" / uses-feature android:name="android.hardware.camera" / uses-feature android:name="android.hardware.camera.autofocus" /

  4. write the code which I just posted

  5. build the app

Did I forgot something or why did it went wrong?

1
Yes, you forgot something: Please read the documentation before coding. You have to wait for the deviceready event before you can use the camera in your app. npmjs.com/package/cordova-plugin-cameraJoerg

1 Answers

0
votes

If you want to receive base64 data (which it looks like you do) please use destinationType.DATA_URL instead of destinationType.FILE_URI.