In Android 4.4, using Camera API getPicture API will result in a enlarged dialog.
The code is below:
var sourceType = pictureSource.SAVEDPHOTOALBUM;
navigator.camera.getPicture(function(fileURI) {
// Success callback
}, function(message) {
// Fail callback
}, {quality: 50, destinationType: destinationType.FILE_URI, sourceType: sourceType, mediaType: mediaType, correctOrientation: false, saveToPhotoAlbum: true});
Change android:anyDensity property in the AndroidManifest.xml from false to true fix the problem
Related
I am working on ionic v1 Camera app after setting up everything correctly (as done many times before). after building app and debugging it i got this error.
Plugin installed correctly,
ng-cordova.min included.
$CordovaCamera injected.
Here is my Code:
$scope.takePhoto = function () {
var options = {
quality: 100,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.CAMERA,
allowEdit: true,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 300,
targetHeight: 300,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: true
};
$cordovaCamera.getPicture(options).then(function (imageData) {
$rootScope.imgURI = "data:image/jpeg;base64," + imageData;
$state.go('menu.signUp');
}, function (err) {
// An error occured. Show a message to the user
});
}
functions are working fine. but camera is not opening and gives error
as seen in picture.
Try with injecting $cordovaCamera in contact controller and use it like this
camera.getPicture() //just camera instead of $cordovaCamera
Use camera.getPicture() or navigator.camera.getPicture()
https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-camera/
Make sure $cordovaCamera dependency Injected then use it like this
$cordovaCamera.getPicture(options).then(function(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
}, function(err) {
// error
});
Cordova version is: 6.3.0
Cordova Android version is: 5.0.0
Camera Plugin version: cordova-plugin-camera 2.2.0 "Camera"
My code works on multiple devices with Android 5.0 and Android 6.0 but it does not working on Android 4.4, Android 4,3 etc.
In Android 4.4 and 4.3 camera plugin triggers both camera and library but it does not get into .then(function(ImageData){..} it goes into error function.
$scope.gallery = function() {
var options = {
quality : 80,
destinationType : Camera.DestinationType.DATA_URL,
sourceType : Camera.PictureSourceType.PHOTOLIBRARY,
allowEdit : false,
encodingType: Camera.EncodingType.JPEG,
popoverOptions: CameraPopoverOptions,
targetWidth: 500,
targetHeight: 500,
saveToPhotoAlbum: true,
correctOrientation: true
};
$cordovaCamera.getPicture(options).then(function(imageData) {
console.log('image');
imaged = imageData;
$scope.statusMessagePhoto = "photo saved ✓";
}, function(error) {
console.log(error.message);
alert(error.message);
});
};
$scope.savephoto = function(){
var options = {
quality : 80,
destinationType : Camera.DestinationType.DATA_URL,
sourceType : Camera.PictureSourceType.CAMERA,
allowEdit : false,
encodingType: Camera.EncodingType.JPEG,
popoverOptions: CameraPopoverOptions,
targetWidth: 500,
targetHeight: 500,
saveToPhotoAlbum: true,
correctOrientation: true
};
$cordovaCamera.getPicture(options).then(function(imageData) {
imaged = imageData;
$scope.statusMessagePhoto = "photo saved ✓";
} , function(error) {
console.log(error.message);
alert(error.message);
});
};
When i press savephoto button camera opens, but in console (log) i get the error "undefined" and the alert too, before even save or choose the photo i want.
Have anyone experienced the same error on Android 4.3 or 4.4. I repeat the code works perfect on Android 5.0 and above.
SOLVED!
remove this line from config.xml
<preference name="AndroidLaunchMode" value="singleInstance"/>
Using ionic plugin "name": "cordova-plugin-camera","version": "2.2.0". I am not getting front facing camera but opens back facing camera, also tried with cameraDirection : 1 as described on the link but it results in back facing camera. i have tested on Android 4.4 and 5.1
is there any way to get front facing camera open by switching or any other way or any fix?
Also debugged straight inside camera plugin code as on the link here i also got cameraDirection = 1 which is for front facing camera, but unable to resolve the problem.
$ionicPlatform.ready(function() {
//camera settings
$rootScope.options = {
quality: 50,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.CAMERA,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 100,
targetHeight: 100,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false,
correctOrientation:true,
cameraDirection : Camera.Direction.FRONT
};
$cordovaCamera.getPicture($rootScope.options).then(function cameraSuccess(imageData) {
$rootScope.imageData = imageData;
},function cameraError(err){
console.log(err);
$rootScope.imageData = '';
})
})
According to Docs you have to pass number in cameraDirection
So in your code write as follow:
$rootScope.options = {
quality: 50,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.CAMERA,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 100,
targetHeight: 100,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false,
correctOrientation:true,
cameraDirection : 1 // 0 means BACK, 1 means FRONT
};
I am developing an App in Android for camera application. I add the camera using cordova plugin
config.xml
<feature name="Camera">
<param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
</feature>
code for taking Picture
function snapPicture () {
navigator.camera.getPicture (onSuccess, onFail,
{ quality: 100,
sourceType: navigator.camera.PictureSourceType.CAMERA,
mediaType: navigator.camera.MediaType.PICTURE,
destinationType: destinationType.FILE_URI,
encodingType: navigator.camera.EncodingType.JPEG,
correctOrientation: false,
saveToPhotoAlbum: true
});
//A callback function when snapping picture is success.
function onSuccess (imageData) {
var image = document.getElementById ('picture');
alert("path : "+imageData);
image.src = imageData;
}
//A callback function when snapping picture is fail.
function onFail (message) {
alert ('Error occured: ' + message);
}
}
The code is working fine in all Android version expect Android Kitkat. In Kitkat am getting the response as "Error capturing image"
can any tell me what is the issue in Kitkat
Thanks in Advance ...!
You made a mistake inside your code. destinationType: destinationType.FILE_URI, will not work. Change that line to destinationType: Camera.DestinationType.FILE_URI, instead and it'll run. Here's your full working code:
function snapPicture() {
navigator.camera.getPicture(onSuccess, onFail, { quality: 100,
sourceType: navigator.camera.PictureSourceType.CAMERA,
mediaType: navigator.camera.MediaType.PICTURE,
destinationType: Camera.DestinationType.FILE_URI,
encodingType: navigator.camera.EncodingType.JPEG,
correctOrientation: false,
saveToPhotoAlbum: true
})
//A callback function when snapping picture is success.
function onSuccess (imageData) {
var image = document.getElementById ('picture');
alert("path : "+imageData);
image.src = imageData;
}
//A callback function when snapping picture is fail.
function onFail (message) {
alert ('Error occured: ' + message);
}
}
I recommend you to use GapDebug to Debug your Applications.
Trying to create a simple app that saves a photo to the phone's Photo Gallery. The photo doesn't appear to get saved to the Gallery, but I don't have any error messages. Using steroids as well.
Here's my application.js:
function cameraGetPicture() {
navigator.camera.getPicture(imageReceived, cameraFail, {
quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
allowEdit: false,
correctOrientation: true,
targetWidth: 600
});
}
function imageReceived(imageURI) {
var image = document.querySelector('img#myImage');
image.src = imageURI;
imageURI = new steroids.File({
path: "image.png",
relativeTo: steroids.app.userFilesPath
});
}
function cameraFail(message) {
alert("Camera error: " + message);
}
Just add following property to Object which you are passing to navigator.camera.getPicture()
saveToPhotoAlbum: true
Your code will look like:
navigator.camera.getPicture(imageReceived, cameraFail, {
quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
allowEdit: false,
correctOrientation: true,
targetWidth: 600,
saveToPhotoAlbum: true
});
This works for me in both android & iOS. Hopefully this will work for you.