Getting errors while loading Deleted images in Titanium ImageView
I am using Titanium SDK 3.4.0 GA while developing an android app that downloads remote images from my webserver to ImageView.
The problem occurs when the device loses connection while loading these images, so I need a way to catch this error (timeout, 404 ...) and set "imageNotAvailable". I am using Network Link Conditioner on MacOSX to reproduce this scenario with low latency, packet loss ...
To prove it, I am using the following code in my test.js (Alloy Controller) and a simple ImageView with id = 'imageView' in my test.xml.
Sometimes throws an exception:
TiDownloadManager: (pool-4-thread-1) [45929.118581] Fix download http: // ...
but not always (the remote connection timeout seems to be infinite), anyway with this exception and without it I cannot catch it (possibly due to an asynchronous request) and does not fire the ERROR event.
function imageNotAvailable(e)
{
Ti.API.info('Error loading image:'+JSON.stringify(e));
$.imageView.image = "/imageNotAvailable.png";
}
function onLoad(e)
{
Ti.API.info('Image Loaded:'+JSON.stringify(e));
}
function setImageAndroid(image)
{
try{
$.imageView.image = 'http://....';
}catch(e){
$.imageView.fireEvent("error");
}
$.imageView.addEventListener("error", imageNotAvailable);
$.imageView.addEventListener("load", onLoad);
}
Sorry my bad english! Thank!
source to share
I can suggest a way to use http request
create a function in some library class that accepts a url you should go to
Connection.getImage = function(link){
var xhr = Titanium.Network.createHTTPClient({
onload : function(e) {
var responseResult = this.responseText;
callback(responseResult);
},
// function called when an error occurs, including a timeout
onerror : function(e) {
callback("fail");
// Ti.API.info('IN ERROR ' + e.error);
},
timeout : 5000
});
xhr.open("GET", link);
xhr.send();
};
// ps you will get a blob response in a response that you can attach to the imageView.blob property and will prompt you to use the default image which will act as a placeholder until you get a success response.
source to share