apache / cordova-plugin-camera

Apache Cordova Plugin camera
https://cordova.apache.org/
Apache License 2.0
966 stars 1.55k forks source link

On Android navigator.camera is undefined (new) #701

Closed Alessandro-PM closed 3 years ago

Alessandro-PM commented 3 years ago

Bug Report

On Android navigator.camera is undefined

Problem

calling navigator.camera.getPicture results in a "Cannot read property 'getPicture' of undefined"

What is expected to happen?

the call should open Camera or folder where images are present

What does actually happen?

a javascript runtime error is produced: Cannot read property 'getPicture' of undefined

Information

I have a cordova app (Phonegap), a file newpost.js included in index.html file and a button that the user press on the form to add a picture. This app uses this plugin since I made it (5 years ago), and I think only lately the problem has occurred.

Command or Code

function getPhoto3() {
try { navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: PIC_Q, destinationType: destinationType.DATA_URL, targetWidth: PIC_PROF_WIDTH, targetHeight: PIC_PROF_WIDTH, sourceType: pictureSource.PHOTOLIBRARY,correctOrientation:true }); } catch (e) { DoAlert(e.message); } }

Environment, Platform, Device

Cordova/Phonegap, on platform Android end on my device Samsung S9, my android emulator (pixel 2 api 29 android 10 x86)

Version information

Cordova: cli-9.0.0 (Android 8.0.0) Plugins:

Build by Phonegap site

Checklist

  • [ x] I searched for existing GitHub issues
  • [ ] I updated all Cordova tooling to most recent version
  • [x ] I included all the necessary information above
breautek commented 3 years ago

a javascript runtime error is produced: Cannot read property 'getPicture' of undefined

Generally this kind of error occurs if you try to access the API before the deviceready event fires. Ensure that you only call plugin APIs after the deviceready event has fired.

Build by Phonegap site

I would also strongly consider moving away from Phonegap technologies and use the cordova CLI instead. PhoneGap is no longer maintained. As of Oct 2020, Adobe has completely disabled their build services, so building via Phonegap is no longer possible. Additionally the Phonegap CLI doesn't allow you to use the latest cordova releases.

Alessandro-PM commented 3 years ago

hello I added document.addEventListener("deviceready", function(){ alert('ready');}, false); in the head section of my index.html and after a build, this is the very first thing I see (the alert) when I open the app. So yes, I call the button after deviceready

DavidGoricanec commented 3 years ago

I am currently running into the same issue. I have set up an alert(navigator.camera); which is getting called on the deviceready-event and it always returns undefined. I've been trying to bugfix it all day but without success. I also tried adding/removing platforms and reinstalling the plugin but without success.

whria78 commented 3 years ago

I have the same issue. "navigator.camera" returns null or undefined.

Alessandro-PM commented 3 years ago

perfect, nobody ca help us?

whria78 commented 3 years ago

I found that camera works well on Monaca. I guess the problem may be related with the phonegap which is deprecated.

Alessandro-PM commented 3 years ago

mmm....I don't think this is the problem. Did you built the same project? anyway can I try to do the same? I mean, the same phonegap zip file could be built in monaca?

timbru31 commented 3 years ago

Please reproduce the issue with actual Cordova tooling, not PhoneGap's outdated CLI, nor Monaco, VoltBuilder, ...

Otherwise we can't assist.

jcesarmobile commented 3 years ago

Latest version of the plugin requires cordova-android 9, so on cordova-android 8 it fails to install and it's not available It's not a camera plugin bug, you just need to update your cordova-android version.

But since you use phonegap build, it won't allow you to use latest cordova-android, you should migrate to something else, it's officially abandoned, not sure why they still allow users to build apps from there, it was supposed to be shut down last October.

https://blog.phonegap.com/update-for-customers-using-phonegap-and-phonegap-build-cc701c77502c