我是一个新手程序员,我正在制作一个Android程序,在给定的URL上显示ImageView上的图像.我的问题是你如何在AsyncTask上使用它?
这些代码适用于最小的SDK 2.2,但我切换到min SDK 3.0,因此它需要在AsyncTask上运行.谢谢您的帮助!:)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
satellite(); //satellite's image from main menu
}
//******satellite url
private void satellite() {
// TODO Auto-generated method stub
ImageView imgView =(ImageView)findViewById(R.id.satellite);
Drawable drawable = LoadImageFromWeb("http://www.pagasa.dost.gov.ph/wb/sat_images/satellite.gif");
imgView.setImageDrawable(drawable);
}
private Drawable LoadImageFromWeb(String url){
try{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)