Reputation: 2511
i have searched for this exception everywhere but could not find a resolution & any help would be appreciated. i have tried putting break points, but they do not get hit, the error is also visible in log.v and not in log.e. The code works for first few calls say for 10-12 times, then gets slower(starts failing with this error), and eventually throws this error every-time.
_actionRunble = new Runnable() {
public void run() {
try{
##..##
_imView.setImageBitmap(bmImg);
Drawable oldD = _imView.getBackground();
Drawable dd = new BitmapDrawable(bmImg);
_imView.setBackgroundDrawable(dd);
//(((BitmapDrawable)oldD).getBitmap()).recycle();
Thread t = new Thread(_r);
t.start();
}catch(Exception e)
{
e.printStackTrace();
}
}
};
_r = new Runnable() {
@Override
public void run() {
downloadFile(imageUrl);
}
};
Bitmap bmImg;
void downloadFile(String fileUrl){
URL myFileUrl =null;
try {
myFileUrl= new URL(fileUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
try {
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
//this.runOnUiThread(_actionRunble);
_mHandler.postDelayed(_actionRunble, 2000);
//_mHandler.postAtFrontOfQueue(_actionRunble);
//_mHandler.post(_actionRunble);
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
activity oncreate calls downloadfile(...) and after return of the call i again call with the same url to get the updated image. I tried delaying the posting message on main queue by 2 secs(although i dont want that) but that doesnt works too :( . Please feel free for further clarification.
Upvotes: 1
Views: 13564
Reputation: 2511
well i resolved it finally. To get rid of this exception in particular, i started using a single HttpURLConnection(reuse it until it fails, where u create it again), BUT with that i ran into "BitmapFactory.decodeStream returning null" problem, while doing "bmImg = BitmapFactory.decodeStream(is); " This was due to the fact i cannot use the same inputstream over the same connection again, so i had to close it before using it again(is.close(), in.close()). but that dint work for some reason(i dont know!). finally instead of getting inputstream from HttpURLConnection (using conn.getInputStream() ) i directly get it from URL ( myFileUrl.openStream() ). BitmapFactory.decodeStream(is) still can return null sometimes(better to handle that case-ask me why :) ), in which case i try the download again. here is the updated downloadFile(...) method, Hope it helps someone :)
void downloadFile(String fileUrl){
URL myFileUrl =null;
try {
myFileUrl= new URL(fileUrl);
} catch (MalformedURLException e) {
//print exception;
}
catch (Exception e) {
//print exception;
}
try {
//InputStream in = conn.getInputStream();--avoid this, get it from url directly instead, unless u really need to read from httpconnection because u want to configure headers or some other reason.
InputStream in = myFileUrl.openStream();
InputStream is = new BufferedInputStream(in);
bmImg = BitmapFactory.decodeStream(is);
in.close();
is.close();
if(bmImg==null)
{
downloadFile(fileUrl);
return;
}
//this.runOnUiThread(_actionRunble);
//_mHandler.postAtFrontOfQueue(_actionRunble);
//_mHandler.post(_actionRunble);
_mHandler.postDelayed(_actionRunble, 1000);
}
catch (IOException e) {
downloadFile(fileUrl);
//print exception;
}
catch (Exception e) {
downloadFile(fileUrl);
//print exception;
}
}
Upvotes: 2
Reputation: 64700
Your problem is with imageUrl
: it probably has to start with http or https, and the actual server or hostname you're connecting to must be either a valid IP address or must name-resolve correctly to an IP address.
Upvotes: 0