1
votes

I make an apps using Appcelerator (Titanium SDK). and I have a problem when open camera, I already set camera permission in tiapp.xml. And I've try use a source from kitchen Sink titanium.

Here's my code

var win;

function fireUpTheCamera() {
    if (Ti.Platform.osname === 'android'|| Ti.Platform.osname == "iphone" || Ti.Platform.osname == 'ipad') {
        win.removeEventListener('focus', fireUpTheCamera);
    }
    Titanium.Media.showCamera({

        success:function(event) {
            var cropRect = event.cropRect;
            var image = event.media;

            Ti.API.debug('Our type was: '+event.mediaType);
            if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
            {
                var imageView = Ti.UI.createImageView({
                    width:win.width,
                    height:win.height,
                    image:event.media
                });
                win.add(imageView);
            }
            else
            {
                alert("got the wrong type back ="+event.mediaType);
            }
        },
        cancel:function() {
        },
        error:function(error) {
            // create alert
            var a = Titanium.UI.createAlertDialog({title:'Camera'});

            // set message
            if (error.code == Titanium.Media.NO_CAMERA)
            {
                a.setMessage('Please run this test on device');
            }
            else
            {
                a.setMessage('Unexpected error: ' + error.code);
            }

            // show alert
            a.show();
        },
        saveToPhotoGallery:true,
        allowEditing:true,
        mediaTypes:[Ti.Media.MEDIA_TYPE_PHOTO]
    });
}

function cam_basic(_args) {
    win = Titanium.UI.createWindow({
        title:_args.title
    });
    if (Ti.Platform.osname === 'android'|| Ti.Platform.osname == "iphone" || Ti.Platform.osname == 'ipad') {
        win.addEventListener('focus', fireUpTheCamera);
    } else {
        fireUpTheCamera();  
    }
    return win;
};

module.exports = cam_basic;

when I finish capture picture and press the OK button, it's always restart application without any error message, also in the log.

I'm using SDK 6.0.0GA.

Please give me some help, and what's wrong with my code.

2
when removing code in the callback, does it still work? - Rene Pot
@RenePot I've try to remove the callback, and still force close without any error receive. - Don Vincent

2 Answers

1
votes

Before firing up the camera you have to ask the end user for permissions. I'm using this snippet and it works with Ti-5.4.0.

if(Ti.Media.hasCameraPermissions())
    fireUpTheCamera();
else
{
    Ti.Media.requestCameraPermissions(function(permission)
    {
        if(permission.success)
            fireUpTheCamera();
        else
            alert('Please Provide permission first');
    });
}
0
votes

Since Titanium sdk 5.1 you need to request runtime permission as well to use the camera.

See here: http://docs.appcelerator.com/platform/latest/#!/api/Titanium.Media-method-requestCameraPermissions