问题从网页加载图像(机器人) [英] problem loading image from web (android)
本文介绍了问题从网页加载图像(机器人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public Object fetch(String address) throws MalformedURLException,
IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
private Drawable ImageOperations(Context ctx, String url) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}
catch (Exception e)
{
return null;
}
}
try {
Drawable a =ImageOperations(this,"url"); imgView.setImageDrawable(a);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
这工作,但在罕见ocasions应用程序冻结由于SocketException:ADRESS协议不支持。有什么办法解决这一问题?谢谢
This works, but on rare ocasions the app freezes due to a "SocketException: Adress family not supported by protocol". Is there any way to fix this? Thanks
推荐答案
您正试图下载从UI线程文件...(这就是为什么你的UI冻结)
You are trying to Download a File from the UI Thread...(which is why your UI Freezes)
使用一个单独的线程或的AsyncTask 让你的UI没有按' ŧ冻结。
Use a Seperate Thread or AsyncTask so that your UI doesn't Freeze up.
这应该解决您的问题。
这篇关于问题从网页加载图像(机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文