2016-11-30 83 views
0

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

这里是我的代码

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; 

当我完成拍摄照片,然后按OK按钮,它总是重新启动应用程序没有任何错误信息,还可以在日志。

我正在使用SDK 6.0.0GA。

请给我一些帮助,以及我的代码有什么问题。

+0

当在回调中删除代码时,它仍然工作吗? –

+0

@RenePot我试图删除回调,并仍然强制关闭没有收到任何错误。 –

回答

1

之前发射了摄像头,你要问权限最终用户。我正在使用这个片段,它可以和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

@ gerber-hofman我一直在使用该代码片段并且无法工作,拍照后,应用程序总是崩溃并重新启动。 –

+2

您可以尝试将allowEditing属性设置为false并重试?在Android上设置为true时,我遇到了该属性的问题。 – Garre