Last active
December 10, 2016 00:58
-
-
Save xcash/e0e363e04999ba9c8ad2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Camera Android Fix | |
* | |
* Shows a workaround for bug https://jira.appcelerator.org/browse/TIMOB-12848 | |
* Android: Using the camera on certain devices causes the app to crash | |
* | |
* | |
*/ | |
// Function to manage the taken picture | |
function managePicture(imgfile) { | |
Ti.API.info('COOL! I have my image!!!'); | |
$.mirrorimg.setImage(imgfile); | |
} | |
// function to start the capture | |
function takePicture(e) { | |
if (Titanium.Platform.Android) { | |
var intent = Ti.Android.createIntent({ | |
action: 'android.media.action.IMAGE_CAPTURE', | |
}); | |
// file to save to | |
var file = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, "tmpcam.jpg"); | |
console.log('output-file: ' + file.resolve()); | |
intent.putExtraUri('output', file.resolve()); | |
Ti.API.info('file exist: ' + file.exists()); | |
if (!file.exists()) { | |
file.write(''); | |
} else { | |
Ti.API.info('file size: ' + file.getSize()); | |
} | |
Ti.API.info('file exist: ' + file.exists()); | |
Ti.App.Properties.setBool('waiting_picture', true); | |
$.index.activity.startActivityForResult(intent, function (e) { | |
console.log('called intent callback!!!! e.resultCode: ' + e.resultCode); | |
if (e.resultCode == Titanium.Android.RESULT_OK) { | |
managePicture(file); | |
} | |
Ti.App.Properties.setBool('waiting_picture', false); | |
}); | |
console.log('called startActivityForResult 1'); | |
} else { | |
// legacy for IOS (not yet tested) | |
Titanium.Media.showCamera({ | |
autoHide: true, | |
success: function (cm) { | |
if (cm.success) { | |
console.log('Got img!'); | |
managePicture(cm.media); | |
} | |
}, | |
error: function (e) { | |
} | |
}); | |
} | |
} | |
$.index.open(); | |
// when this bug is happening, Android activity goes through an onCreate -> onStart -> onResume cycle, after getting destroyed (without onDestroy event | |
// we use a persistent key to manage this, like the android native method of using saveState and restoreState. | |
$.index.activity.onCreate = function (e) { | |
console.log('ACTLIFECYCLELOG act-onCreate'); | |
// check if died pending a picture intent | |
if (Ti.App.Properties.getBool('waiting_picture', false)) { | |
Ti.API.info('image capture pending!!!'); | |
var file = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, "tmpcam.jpg"); | |
if ( file.exists() && (file.getSize() > 10) ) { | |
// seems a legit file | |
managePicture(file); | |
Ti.App.Properties.setBool('waiting_picture', false); | |
} | |
} | |
}; | |
// other events for debug | |
$.index.activity.onStart = function (e) { | |
console.log('ACTLIFECYCLELOG act-onStart'); | |
}; | |
$.index.activity.onResume = function (e) { | |
console.log('ACTLIFECYCLELOG act-onResume'); | |
}; | |
$.index.activity.onPause = function (e) { | |
console.log('ACTLIFECYCLELOG act-onPause'); | |
}; | |
$.index.activity.onStop = function (e) { | |
console.log('ACTLIFECYCLELOG act-onStop'); | |
}; | |
$.index.activity.onDestroy = function (e) { | |
console.log('ACTLIFECYCLELOG act-onDestroy'); | |
}; | |
$.index.activity.onRestart = function (e) { | |
console.log('ACTLIFECYCLELOG act-onRestart'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment