【问题标题】:Appcelerator Android Camera always make application force closeAppcelerator Android Camera 总是让应用力关闭
【发布时间】:2016-11-30 10:09:40
【问题描述】:

我使用 Appcelerator (Titanium SDK) 制作应用程序。 打开相机时出现问题,我已经在tiapp.xml中设置了相机权限。 我已经尝试使用厨房水槽钛的来源。

这是我的代码

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;

当我完成捕获图片并按下确定按钮时,它总是重新启动应用程序而没有任何错误消息,也在日志中。

我正在使用 SDK 6.0.0GA。

请给我一些帮助,我的代码出了什么问题。

【问题讨论】:

  • 在回调中删除代码时,它仍然有效吗?
  • @RenePot 我已经尝试删除回调,并且仍然强制关闭而不会收到任何错误。

标签: android titanium appcelerator titanium-mobile


【解决方案1】:

在启动摄像头之前,您必须向最终用户请求权限。我正在使用这个 sn-p,它适用于 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');
    });
}

【讨论】:

  • @gerber-hofman 我一直在用那个sn-p,不工作,拍照后,应用程序总是崩溃并重新启动。
  • 您可以尝试将 allowEditing 属性设置为 false 并重试吗?在 Android 上设置为 true 时,我遇到了该属性的问题。
【解决方案2】:

从 Titanium sdk 5.1 开始,您还需要请求运行时权限才能使用相机。

请看这里:http://docs.appcelerator.com/platform/latest/#!/api/Titanium.Media-method-requestCameraPermissions

【讨论】:

  • 问题应该作为评论添加,而不是答案;)Android也被添加为标签,所以你认为是对的
  • 谢谢。编辑了我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多