2
votes

i am working on a cross-platform mobile app using appcelerator titanium alloy. the code below opens the native camera functionality and works fine on apple devices. on android devices, the camera opens but the button to actually take a picture is disabled (greyed out). the buttons for taking video or changing camera settings work fine, just the take picture button is not working. any ideas? thanks in advance

the code below called on click of takePic button to open camera

takePic.addEventListener('click', function(e) {
    var win = Titanium.UI.createWindow({ //Open Camera

    });
    Titanium.Media.showCamera({
         success:function(event){
            Ti.API.debug('Our type was: '+event.mediaTpe);
            if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO){

                ***win = Titanium.UI.currentWindow;
                if(osName == "android"){
                    win.open();
                }***

                try {                    
                    var image_name = "siteing.jpg";
                    var fileImage = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, image_name);
                    fileImage.write(event.media);
                    Ti.API.log(event.media);

                    picURL = fileImage;//event.media;
                    picRaw = event.media; //Raw bytes of picture to save to database

                    pictureSet = true;
                    $.videoButton.enabled = false;
                    $.videoButton.backgroundColor = "#DDDDDD";
                    //$.audioButton.enabled = false;
                    format = "Picture";
                    $.savePic.show();

                } catch(e){
                    alert("An Error:" + e.message);
                }

            } else {
                var alert = createErrorAlert({text:"An error occured getting the media. Please check file size and format and try again."});
                $.yesLatLon.add(alert);
                alert.show();
            }
            if(osName == "android"){
                win.open();
            }
        }, cancel:function(){
                 //called when user cancels taking a picture
                 if(osName == "android"){
                    //win.close();
                }
             }, error:function(error){
                 //called when there's an error
                 var a = Titanium.UI.createAlertDialog({title:'Camera'});
                 if(error.code == Titanium.Media.NO_CAMERA){
                     a.setMessage('Please run this test on device');
                 } else {
                     a.setMessage('Unexpected error: ' + error.code);
                 }
                 a.show();
             }, saveToPhotoGallery:true,
             //allowEditing and mediaTypes are iOS-only settings
        allowEditing:true,
        mediaTypes:[Ti.Media.MEDIA_TYPE_VIDEO, Ti.Media.MEDIA_TYPE_PHOTO]
     });
     alert.hide();
     $.yesLatLon.removeEventListener('androidback', arguments.callee);
});

screenshot_from_phone

1

1 Answers

1
votes

It's been a long time, but I did get this working and thought I'd post my code in case it helps someone else. Fair warning, I now face a different issue with the camera on certain Android devices (Samsung S-series and Google Pixel cannot open camera), which I'm posting in a separate question. However, the original issue with camera button being disabled was successfully resolved last year with the below code.

takePic.addEventListener('click', function(e) {
        var win = Titanium.UI.createWindow({//Open Camera

        });

        Titanium.Media.showCamera({
            success : function(event) {
                if (event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) {

                    win = Titanium.UI.currentWindow;

                    if (osName == "android") {
                        var img_view = Titanium.UI.createImageView({
                            height : '100%',
                            width : '100%',
                        });

                        win.add(img_view);
                    }
                    try {
                        picURL = event.media;
                        picRaw = event.media;
                        pictureSet = true;
                        $.videoButton.enabled = false;
                        $.videoButton.backgroundColor = "#DDDDDD";
                        $.savePic.show();
                        format = "Picture";
                    } catch(e) {
                        alert("An Error:" + e.message);
                    }

                } else {
                    var alert = createErrorAlert({
                        text : "An error occured getting the media. Please check file size and format and try again."
                    });
                    $.yesLatLon.add(alert);
                    alert.show();
                }

            },
            cancel : function() {
                //called when user cancels taking a picture

            },
            error : function(error) {
                //called when there's an error
                var a = Titanium.UI.createAlertDialog({
                    title : 'Camera'
                });
                if (error.code == Titanium.Media.NO_CAMERA) {
                    a.setMessage('Please run this test on device');
                } else {
                    a.setMessage('Unexpected error: ' + error.code);
                }
                a.show();
            },
            saveToPhotoGallery : true,
            allowEditing : false,
            autohide : true, //Important!
            mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO]
        });
        alert.hide();
    });