我有 UI 组件,其中有包含 ImageViews 的表,现在我想从 URL 设置图像。
现在,当我尝试异步调用图像设置为 UI 的另一个类时,它给了我一个错误,即 UI 只能由 UI 线程更新。我想加载 UI 并在可用时加载图像。
这是我的代码:
Calling function...
new AsyncImageLoader(context,imgviewArray).execute();
// Called class..
@Override
protected Void doInBackground(Void... urls) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
int i = 0;
try{
db=new MyDB(mainact_instance);
System.out.println("Inside doInBackground");
if(isNetworkAvailable())
{
System.out.println("Network connected");
System.out.println("Fetching from network-Images");
Movie[] movies = db.selectRecords();
for(Movie mv : movies)
{
try {
URL url = new URL(IMAGE_URL+mv.getId()+".jpg");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
images[i].setImageBitmap(bmp);
i++;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else{
System.out.println("Network not connected..fetching max id row");
}
}catch(Exception ex){ex.printStackTrace();
}
return null;
//db.close();
}
请告诉我如何实现这一目标?