I've been wracking my brains on this for three weeks now. I'd appreciate any insights.
The only aspect that is consistent is that the error happens after an image is downloaded is written to the device's memory with the write command.
Unfortunately the same image that crashed the application before will work fine in later attempts - even after emptying the application's cache in the Android system preferences.
This is both and iOS / Android application. Images are downloaded before being presented in an image view because there are some differences in how both platforms must access the image before extracting height and width data. There is also a queue system on board to monitor the amount of images that are being processed at any one time.
Currently only one image can be downloaded and processed at any one time.
This application does not have any issues running in iOS. This is an Android only error.
See "THE CRASH HAPPENS INTERMITTENTLY ON THE NEXT LINE" in the code below. When the crash happens, the application force closes on f.write.
TECH
Application type: mobile Titanium SDK: 3.0.2.GA Platform & version: Android SDK 11 Device: Razr M Host OS: Android ICS 4.1.1 Titanium Studio: build: 3.0.2.201302191606
CODE
py.ui.cachedImageView = function(basedir, uri, place, cacheage) { var filename = py.ui.imageQueue[place]["url"].split('/'); filename = filename[filename.length - 1]; if (py.ui.imageQueue[place]['pass'] == 2) filename = filename + "_hd"; var d = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, basedir); if (!d.exists()) { d.createDirectory(); } var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, basedir, filename); if (f.exists()) { Ti.App.fireEvent('py.ui.cachedImageViewLoaded',{'loaded': true, 'place': place, 'filename': filename, 'basedir': basedir, 'status': "200"}); } else { // retrieve remote image. var req = Ti.Network.createHTTPClient(); req.setTimeout(5000); req.open('GET', uri); req.onload = function() { if (req.readyState == 4 && req.status == 200) { Ti.API.info("ReponseHeader Type: " + req.getResponseHeader("Content-Type")); var responseType = req.getResponseHeader("Content-Type").split("/"); if (responseType[0] == "image" && req.responseData){ Ti.API.info("writing URI to file: " + uri); Ti.API.info("To Directory: " + Ti.Filesystem.applicationDataDirectory + basedir + "/" + filename); // THE CRASH HAPPENS INTERMITTENTLY ON THE NEXT LINE f.write(req.responseData); Ti.API.info("Write Successful"); // disregard if imageQueue has been removed during the time it took to connect if (py.ui.imageQueue.length == 0) return; Ti.App.fireEvent('py.ui.cachedImageViewLoaded',{'loaded': true, 'place': place, 'filename': filename, 'basedir': basedir, 'status': req.status}); } else { Ti.App.fireEvent('py.ui.cachedImageViewLoaded',{'loaded': false, 'place': place, 'filename': filename, 'basedir': basedir, 'status': 404}); } } else { Ti.App.fireEvent('py.ui.cachedImageViewLoaded',{'loaded': false, 'place': place, 'filename': filename, 'basedir': basedir, 'status': req.status}); } } req.onerror = function(){ Ti.App.fireEvent('py.ui.cachedImageViewLoaded',{'loaded': false, 'place': place, 'filename': filename, 'basedir': basedir, 'status': req.status}); } req.send(); } }
LOGS
03-05 15:05:59.134: D/TiProperties(2204): (KrollRuntimeThread) [1,52290] getInt called with key:ti.android.httpclient.maxbuffersize, def:524288 03-05 15:05:59.134: D/TiHttpClient(2204): (KrollRuntimeThread) [1,52291] open request method=GET url=http://lh3.ggpht.com/EtDJ4XoZIB1J043iVInI1hEH9rYL9udIqL7CIyzbL9oXVGb6RiLy6_4cWYhypzkRQ80tJ2LBJfeNRCQ6ipf63g=w267?0.8721161696594208 03-05 15:05:59.134: D/TiHttpClient(2204): (KrollRuntimeThread) [1,52292] Instantiating host with hostString='lh3.ggpht.com', port='-1', scheme='http' 03-05 15:05:59.134: D/TiHttpClient(2204): (KrollRuntimeThread) [0,52292] Setting ready state to 1 03-05 15:05:59.134: D/TiHttpClient(2204): (KrollRuntimeThread) [0,52292] Instantiating http request with method='GET' and this url: 03-05 15:05:59.134: D/TiHttpClient(2204): (KrollRuntimeThread) [0,52292] http://lh3.ggpht.com/EtDJ4XoZIB1J043iVInI1hEH9rYL9udIqL7CIyzbL9oXVGb6RiLy6_4cWYhypzkRQ80tJ2LBJfeNRCQ6ipf63g%3Dw267?0.8721161696594208 03-05 15:05:59.144: D/TiHttpClient(2204): (KrollRuntimeThread) [1,52293] Leaving send() 03-05 15:05:59.395: I/TiAPI(2204): ReponseHeader Type: image/jpeg 03-05 15:05:59.395: I/TiAPI(2204): writing URI to file: http://lh3.ggpht.com/EtDJ4XoZIB1J043iVInI1hEH9rYL9udIqL7CIyzbL9oXVGb6RiLy6_4cWYhypzkRQ80tJ2LBJfeNRCQ6ipf63g=w267?0.8721161696594208 03-05 15:05:59.395: I/TiAPI(2204): To Directory: appdata-private://detailcache/EtDJ4XoZIB1J043iVInI1hEH9rYL9udIqL7CIyzbL9oXVGb6RiLy6_4cWYhypzkRQ80tJ2LBJfeNRCQ6ipf63g 03-05 15:05:59.395: A/libc(2204): Fatal signal 11 (SIGSEGV) at 0x0000000c (code=1), thread 2248 (KrollRuntimeThr)