我到处寻找这个例外,但找不到解决方案&任何帮助将不胜感激。
我尝试过设置断点,但它们没有被击中,该错误也在 log.v 中可见,而不是在 log.e 中可见。
该代码在前几次调用中有效,例如 10-12 次,然后变得更慢(开始因此错误而失败),并最终每次都会抛出此错误。
_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();
}
}
活动 oncreate 调用 downloadfile(...) ,在调用返回后,我再次使用相同的 url 进行调用以获取更新的图像。我尝试将主队列上的发布消息延迟 2 秒(尽管我不希望如此),但这也不起作用:(。
请随时进一步澄清。
好吧,我终于解决了。
为了特别摆脱这个异常,我开始使用单个 HttpURLConnection(重复使用它直到失败,然后再次创建它),但是在执行“bmImg = BitmapFactory”时遇到“BitmapFactory.decodeStream 返回 null”问题.decodeStream(is);
“这是因为我无法再次在同一个连接上使用相同的输入流,所以我必须在再次使用它之前关闭它(is.close(),in.close())。但由于某种原因,这种作用不起作用(我不知道!)。
最后,我没有从 HttpURLConnection 获取输入流(使用 conn.getInputStream() ),而是直接从 URL( myFileUrl.openStream() )获取它。 BitmapFactory.decodeStream(is) 有时仍然可以返回 null(更好地处理这种情况 - 问我为什么:)),在这种情况下我会再次尝试下载。
这是更新的 downloadFile(...) 方法,希望它对某人有帮助:)
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;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)