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.
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 🙂