2016-11-30 84 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