[TIMOB-17951] Calling Ti.Media.switchCamera(undefined) yields unexpected results on devices with 1 or no camera
| GitHub Issue | n/a |
| Type | Improvement |
| Priority | Low |
| Status | Closed |
| Resolution | Fixed |
| Resolution Date | 2016-01-06T18:23:48.000+0000 |
| Affected Version/s | Release 3.3.0, Release 3.4.0 |
| Fix Version/s | Release 5.4.0 |
| Components | Android, iOS |
| Labels | camera, media, qe-3.4.1, runtime |
| Reporter | Eric Wieber |
| Assignee | Hans Knöchel |
| Created | 2014-11-03T21:56:07.000+0000 |
| Updated | 2016-07-13T18:33:45.000+0000 |
Description
When Ti.Media.switchCamera(undefined); is called on a device/simulator with zero or one camera, a runtime error is thrown.
If Ti.Media.switchCamera(undefined); is called on a device/simulator with two camera's (and one has been opened) then no action is performed and no error occurs.
*This is not a regression*.
*Steps to reproduce*:
1. Build a project with the attached files, for a device with 1 or no camera
2. Click through the camera test buttons
3. Notice that they correctly open the camera or not, depending on if that camera exists on the device
3. Click on the "Invalid Camera" button
4. Notice that a runtime error is thrown
*Expected Result*:
No error is thrown and no action is performed (like the camera buttons for unsupported/nonexistent cameras).
*Notes*:
I was unable to encounter this error on devices with two cameras, despite the same code and 'undefined' parameter passed.
Attachments
| File | Date | Size |
| Archive.zip | 2014-11-03T21:56:07.000+0000 | 1045 |
[~ewieber] Both iOS and Android? EDIT: After some research on the existing code, -I don't think we should handle this edge case explicitly-. The method expects a constant of either
Ti.Media.CAMERA_FRONTorTi.Media.CAMERA_REAR, the developers should know that before using the method. Anyway, I would adjust the docs, since they list some constants that are not allowed there ([see here](http://docs.appcelerator.com/platform/latest/#!/api/Titanium.Media-method-switchCamera)).PR: https://github.com/appcelerator/titanium_mobile/pull/7598 Demo
var window = Ti.UI.createWindow({layout: "vertical"}); var btn1 = createButtonWithTitleAndAction("Default Camera", defCam); var btn2 = createButtonWithTitleAndAction("Front Camera 1", frontCam1); var btn3 = createButtonWithTitleAndAction("Front Camera 2", frontCam2); var btn4 = createButtonWithTitleAndAction("Back Camera 1", backCam1); var btn5 = createButtonWithTitleAndAction("Back Camera 2", backCam2); var btn6 = createButtonWithTitleAndAction("Invalid camera", invCam); window.add(btn1); window.add(btn2); window.add(btn3); window.add(btn4); window.add(btn5); window.add(btn6); window.open(); function defCam() { Ti.Media.showCamera({ mediaType : Ti.Media.MEDIA_TYPE_PHOTO }); Ti.Media.vibrate(); }; function frontCam1() { Ti.Media.showCamera({ mediaType : Ti.Media.MEDIA_TYPE_PHOTO }); Ti.Media.switchCamera(Ti.Media.CAMERA_FRONT); Ti.Media.vibrate(); }; function frontCam2() { Ti.Media.showCamera({ mediaType : Ti.Media.MEDIA_TYPE_PHOTO }); Ti.Media.switchCamera(Ti.Media.CAMERA_REAR); Ti.Media.switchCamera(Ti.Media.CAMERA_FRONT); Ti.Media.vibrate(); }; function backCam1() { Ti.Media.showCamera({ mediaType : Ti.Media.MEDIA_TYPE_PHOTO }); Ti.Media.switchCamera(Ti.Media.CAMERA_REAR); Ti.Media.vibrate(); }; function backCam2() { Ti.Media.showCamera({ mediaType : Ti.Media.MEDIA_TYPE_PHOTO }); Ti.Media.switchCamera(Ti.Media.CAMERA_FRONT); Ti.Media.switchCamera(Ti.Media.CAMERA_REAR); Ti.Media.vibrate(); }; function invCam() { Ti.Media.showCamera({ mediaType : Ti.Media.MEDIA_TYPE_PHOTO }); Ti.Media.switchCamera(undefined); Ti.Media.vibrate(); }; function createButtonWithTitleAndAction(title, action) { var btn = Ti.UI.createButton({ top: 30, title: title }); btn.addEventListener("click", action); return btn; }Verified the fix. No runtime error is thrown when Ti.Media.switchCamera(undefined) is called. Closing. Environment: Appc Studio : 4.7.0.201607111053 Ti SDK : 5.4.0.v20160712013704 Ti CLI : 5.0.9 Alloy : 1.9.1 MAC El Capitan : 10.11.5 Appc NPM : 4.2.8-1 Appc CLI : 5.4.0-33 Node: 4.4.4 Nexus 6 - Android 6.0.1