简体   繁体   English

Cordova:camera.getPicture 失败并显示“Camera cancelled”

[英]Cordova: camera.getPicture fails with "Camera cancelled"

I'm developing a cordova app (cli-5.2.0) in which pictures are taken using the cordova camera plugin (cordova-plugin-camera, version 1.2.0).我正在开发一个cordova 应用程序(cli-5.2.0),其中使用cordova 相机插件(cordova-plugin-camera,1.2.0 版)拍摄照片。

 var camera = navigator.camera; var cameraOptions = { destinationType: Camera.DestinationType.DATA_URL }; camera.getPicture(function(result) { alert('success'); }, function(error) { alert('fail ' + error); }, cameraOptions);

This code works fine.. ..most of the time.这段代码工作正常......大部分时间。
On a particular slow android 4.4.4 device, this only works fine like 9 out of 10 times.在特定的慢速 android 4.4.4 设备上,这只能正常工作 10 次中的 9 次。
In like 1 out of 10, the call fails saying 'Camera cancelled' (although it was not cancelled with the back button or something).大约 10 分之 1 的呼叫失败,说“相机已取消”(尽管它没有用后退按钮或其他东西取消)。

In that case, the failure callback 'Camera cancelled' is fired, and right after that, the Camera UI is shown nevertheless.在这种情况下,会触发失败回调“Camera cancelled”,紧接着会显示“Camera UI”。
(if a picture is taken then, the success callback is not called). (如果拍摄了照片,则不会调用成功回调)。

It's hard to tell when this happens, but it looks like it happens more often if the device is app is more busy.很难说这种情况何时发生,但如果设备的应用程序更忙,它看起来会更频繁地发生。
The log files do not show much more detail, except for things like:日志文件没有显示更多细节,除了以下内容:

Attempted to send a second callback for ID: Camera1337050609尝试为 ID 发送第二个回调:Camera1337050609

However, these logs are also written if the call succeeds.但是,如果调用成功,也会写入这些日志。


Any hints or help is much appreciated.非常感谢任何提示或帮助。

Thanks for your time,谢谢你的时间,
Koen公园

update 1 (answer to comment)更新 1 (对评论的回答)

 ProjectDetailCtrl.prototype.takePhoto = function () { var _this = this; // this cameraService calls the code from above return this.cameraService.getPicture() .then(function (imageData) { _this.$log.debug('got camera data'); // .. },
 <!-- This is an angular application, the click handling goes like: --> <md-button ng-click="vm.takePhoto()"> Add photo </md-button>

I have seen this problem occur under 2 conditions - 1) Low memory as what @Joerg has already mentioned, 2) Your events are cascaded, and are triggered twice.我已经看到在 2 种情况下会出现此问题 - 1)@Joerg 已经提到的内存不足,2)您的事件是级联的,并被触发两次。 For instance, you have a onClick on an Icon, inside a div that also has another event trigger like an a-href or another onClick.例如,您在图标上有一个 onClick,在一个 div 中,该 div 也有另一个事件触发器,如 a-href 或另一个 onClick。

If you can post your code, it might help.如果您可以发布您的代码,它可能会有所帮助。

Update - 1: Definitely indicates a low memory situation.更新 - 1:绝对表示内存不足的情况。 Hazarding a guess here... 1. Asynch call to Camera occurs 2. Low memory returns a Camera cancelled failure event 3. Camera does open later when enough free memory is available 4. Camera success event is not able to find the callback as the callback is already cleared after the earlier failure event, resulting in 'Second callback' scenario这里有一个危险的猜测... 1. 发生对相机的异步调用 2. 内存不足返回相机取消失败事件 3. 相机在有足够的可用内存时会稍后打开 4. 相机成功事件无法找到回调,因为在较早的失败事件之后回调已经被清除,导致“第二次回调”场景

Now, do you have this problem with the exact same photo/video each time?现在,您每次都遇到完全相同的照片/视频的问题吗?

The way you can probably avoid this is - not chain the call with a ".then" but actually do a regular async callback.您可能可以避免这种情况的方法是 - 不使用“.then”链接调用,而是实际执行常规异步回调。

Also, you might want to check and guard against any earlier invocation of the 'camera service' is finished.此外,您可能希望检查并防止任何较早的“相机服务”调用已完成。

My problem was that I had Angular material installed which provokes that the buttons and other click elements gets fired two times.我的问题是我安装了 Angular 材料,这会导致按钮和其他点击元素被触发两次。 To solve it I had to add this line in my app.js为了解决这个问题,我必须在 app.js 中添加这一行

.config(function($stateProvider, $urlRouterProvider, $mdThemingProvider, $mdGestureProvider) {

$mdGestureProvider.skipClickHijack();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM