I'm trying to use the Cordova native plugins for the first time. I started out with the camera and the sample code provided in the documentation. This is failing however and the navigator.camera
is undefined.
I've included the code below.
I installed the camera plugin according to the CLI directions
cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git
I also added the cordova.js files.
回答1:
Where are you testing your code?
I dont know if you still need the anwser. But I think it can help someone. I run your code and it works 100%. I have tested in Android's emulator with a emulated Camera. I think that you forgot the uses permissions.
I made an example here https://github.com/paulorbpacheco/CameraTest-Cordova.git
I hope this can help you...
Regards
回答2:
This is a bit old, but I wanted to provide hopefully for insight to anyone else that might come along later...
The issue I was running into was caused by the fact I had done an older npm install of cordova at one point that was still on my system, and then later the npm install of phonegap.
Cordova is a dependency of phonegap so the necessary version of cordova was present, but when I would run the plugin install:
cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git
the system was defaulting to the old version of cordova and as a result was not getting the correct version of the plugins.
I was reminded of this issue again recently when I upgraded my npm phonegap package from 3.0.0 to 3.1.0. Once again, the plugins changed and were not backwards compatible. In the case of 3.0.0, PhoneGap expects a plugin directory of
org.apache.cordova.core.camera org.apache.cordova.core.X
In the latest release 3.1.0 the core namespacing was dropped and these now look like this
org.apache.cordova.camera org.apache.cordova.X
In normal use scenarios I don't think this is something people run into on a daily basis. Most projects I'm guessing are being generated and the developer is adding the plugins they need and sticking to a particular release of PhoneGap until at some point maybe they decide to upgrade the project. Where this becomes an issue as I see it is when the developer starts working on multiple PhoneGap projects at the same time; the case I'm having here. I upgraded to a newer version for a new project, then needed to add plugins to the old project EXPLOSION OCCURS
Up to this point I had been using a global install of PhoneGap (per the official documentation at phonegap.com)
sudo npm install -g phonegap
My short term solution ended up being to cp the needed plugin directory from an old project that was using the version I needed over into my other project that happens to be running on the same version. My next steps will be to test out using nvm and see if I'm am able to use multiple installs of PhoneGap this way per project, rather than having an always shifting global version.
回答3:
Undefined issue generated because cordova is not properly builded, so Please follow the steps below in order to resolve navigator.camera issue
Step 1 : Grab latest phonegap zip (http://phonegap.com/install/) , right now latest build is 2.9.1 that contains cordova.2.9.1.js , but doesn't have cordova2.9.1.jar (You need to generate jar file, plz follow step 2)
step 2: Then you need to generate cordova2.9.1.jar , Follow this link below how to generate .jar file Where is cordova-2.7.0.jar?
Remember you have to have proper cordova.2.9.1.js and its respective cordova.2.9.1.jar in your android project in "Eclipse IDE"
Step 3: Don't forget to add camera permission in your config.xml (Please follow config.xml, if not works)
Sky Lite I am Rocking guy. This man rock your day.
Step 4 : Add Permission in your AndroidManifest.xml file
Step 5 : Add code in your own javascript file (Plz check my js Code )
$("#btnImageCaptured").on("click", function(e){ alert("image uploading"); e.preventDefault(); e.stopPropagation(); navigator.camera.getPicture(onSuccessImageAttachment, onFailImageAttachment, {quality:50, destinationType: Camera.DestinationType.FILE_URI, sourceType : Camera.PictureSourceType.CAMERA, //encodingType: navigator.camera.EncodingType.PNG, allowEdit: true, targetWidth: 200, targetHeight: 200}); //targetWidth: 100, targetHeight: 150, //mediaType : Camera.MediaType.PICTURE //sourceType:navigator.camera.PictureSourceType.PHOTOLIBRARY}); }); function onSuccessImageAttachment(imageData){ window.alert("success"); //window.alert("Prishah took Image successfully : " + imageData); window.location.href = "#orderAttachedLotImage"; $("#imgAttachedLot").attr('src','data:image/jpeg;base64,' + imageData); //$("#imgAttachedLot").src = "data:image/jpeg;base64," + imageData; //TODO : Insert new Image request to the server //TODO : Display Image } function onFailImageAttachment(failMsg) { window.alert("Failed to image uploading :" + failMsg); }
Step 6(Optional) : Try turning off multitasking to see if it helps. In your java app, add:
super.setBooleanProperty("keepRunning", false);
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setIntegerProperty("splashscreen", R.drawable.screen_hdpi_portrait); super.setBooleanProperty("keepRunning", false); //For Camera super.loadUrl("file:///android_asset/www/index.html", 2000); //pinch zooming Capabilities WebSettings ws = super.appView.getSettings(); ws.setSupportZoom(true); ws.setBuiltInZoomControls(true); }
回答4:
Use this to supress the camera in the desktop mode
function onDeviceReady() { try { pictureSource = navigator.camera.PictureSourceType; destinationType = navigator.camera.DestinationType; } catch (e) { } }
回答5:
Hi You could try using Icenium
http://www.icenium.com/ and there create a Phonegap
project where there will be already sample application with camera API is done and you could find the code there.
Hope this helps